init commit
BIN
icons/baby.png
Normal file
|
After Width: | Height: | Size: 5.8 KiB |
BIN
icons/banana.png
Normal file
|
After Width: | Height: | Size: 7.8 KiB |
BIN
icons/bed.png
Normal file
|
After Width: | Height: | Size: 2.3 KiB |
BIN
icons/boat.png
Normal file
|
After Width: | Height: | Size: 5 KiB |
BIN
icons/book.png
Normal file
|
After Width: | Height: | Size: 7 KiB |
BIN
icons/bottle.png
Normal file
|
After Width: | Height: | Size: 1.6 KiB |
BIN
icons/bow.png
Normal file
|
After Width: | Height: | Size: 5.7 KiB |
BIN
icons/bowl.png
Normal file
|
After Width: | Height: | Size: 1.6 KiB |
BIN
icons/bread.png
Normal file
|
After Width: | Height: | Size: 2.3 KiB |
BIN
icons/brush.png
Normal file
|
After Width: | Height: | Size: 5 KiB |
BIN
icons/camera.png
Normal file
|
After Width: | Height: | Size: 4.2 KiB |
BIN
icons/candy.png
Normal file
|
After Width: | Height: | Size: 4 KiB |
BIN
icons/car.png
Normal file
|
After Width: | Height: | Size: 5.8 KiB |
BIN
icons/cat.png
Normal file
|
After Width: | Height: | Size: 5.8 KiB |
BIN
icons/chain.png
Normal file
|
After Width: | Height: | Size: 5.7 KiB |
BIN
icons/chalk.png
Normal file
|
After Width: | Height: | Size: 4.9 KiB |
BIN
icons/clock.png
Normal file
|
After Width: | Height: | Size: 6.1 KiB |
BIN
icons/computer.png
Normal file
|
After Width: | Height: | Size: 2.6 KiB |
BIN
icons/cord.png
Normal file
|
After Width: | Height: | Size: 1.7 KiB |
BIN
icons/cork.png
Normal file
|
After Width: | Height: | Size: 4.7 KiB |
BIN
icons/desk.png
Normal file
|
After Width: | Height: | Size: 1.6 KiB |
BIN
icons/door.png
Normal file
|
After Width: | Height: | Size: 6.5 KiB |
BIN
icons/dot.png
Normal file
|
After Width: | Height: | Size: 2.5 KiB |
BIN
icons/elephant.png
Normal file
|
After Width: | Height: | Size: 2.1 KiB |
BIN
icons/eraser.png
Normal file
|
After Width: | Height: | Size: 2.9 KiB |
BIN
icons/fan.png
Normal file
|
After Width: | Height: | Size: 8 KiB |
BIN
icons/fork.png
Normal file
|
After Width: | Height: | Size: 1.8 KiB |
131
icons/gameServer.java
Normal file
|
|
@ -0,0 +1,131 @@
|
|||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
public class gameServer {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
ServerSocket listener = new ServerSocket(8901);
|
||||
System.out.println("Game Server is Running");
|
||||
try {
|
||||
while (true) {
|
||||
Game game = new Game();
|
||||
Game.Player player1 = game.new Player(listener.accept());
|
||||
Game.Player player2 = game.new Player(listener.accept());
|
||||
player1.setOpponent(player2);
|
||||
player2.setOpponent(player1);
|
||||
game.currentPlayer = player1;
|
||||
player1.start();
|
||||
player2.start();
|
||||
}
|
||||
} finally {
|
||||
listener.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Game {
|
||||
Player currentPlayer;
|
||||
|
||||
// winner
|
||||
public boolean hasWinner() {
|
||||
return
|
||||
(board[0] != null && board[0] == board[1] && board[0] == board[2])
|
||||
||(board[3] != null && board[3] == board[4] && board[3] == board[5])
|
||||
||(board[6] != null && board[6] == board[7] && board[6] == board[8])
|
||||
||(board[0] != null && board[0] == board[3] && board[0] == board[6])
|
||||
||(board[1] != null && board[1] == board[4] && board[1] == board[7])
|
||||
||(board[2] != null && board[2] == board[5] && board[2] == board[8])
|
||||
||(board[0] != null && board[0] == board[4] && board[0] == board[8])
|
||||
||(board[2] != null && board[2] == board[4] && board[2] == board[6]);
|
||||
}
|
||||
|
||||
// no empty squares
|
||||
public boolean boardFilledUp() {
|
||||
for (int i = 0; i < board.length; i++) {
|
||||
if (board[i] == null) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
// thread when player tries a move
|
||||
public synchronized boolean legalMove(int location, Player player) {
|
||||
if (player == currentPlayer && board[location] == null) {
|
||||
board[location] = currentPlayer;
|
||||
currentPlayer = currentPlayer.opponent;
|
||||
currentPlayer.otherPlayerMoved(location);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
class Player extends Thread {
|
||||
char mark;
|
||||
Player opponent;
|
||||
Socket socket;
|
||||
BufferedReader input;
|
||||
PrintWriter output;
|
||||
// thread handler to initialize stream fields
|
||||
public Player(Socket socket) {
|
||||
this.socket = socket;
|
||||
this.mark = mark;
|
||||
try {
|
||||
input = new BufferedReader(
|
||||
new InputStreamReader(socket.getInputStream()));
|
||||
output = new PrintWriter(socket.getOutputStream(), true);
|
||||
output.println("WELCOME " + mark);
|
||||
output.println("MESSAGE Waiting for opponent to connect");
|
||||
} catch (IOException e) {
|
||||
System.out.println("Player died: " + e);
|
||||
}
|
||||
}
|
||||
//Accepts notification of who the opponent is.
|
||||
public void setOpponent(Player opponent) {
|
||||
this.opponent = opponent;
|
||||
}
|
||||
|
||||
|
||||
//Handles the otherPlayerMoved message.
|
||||
public void otherPlayerMoved(int location) {
|
||||
output.println("OPPONENT_MOVED " + location);
|
||||
output.println(
|
||||
hasWinner() ? "DEFEAT" : boardFilledUp() ? "TIE" : "");
|
||||
}
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
// The thread is only started after everyone connects.
|
||||
output.println("MESSAGE All players connected");
|
||||
|
||||
// Tell the first player that it is his/her turn.
|
||||
if (mark == 'X') {
|
||||
output.println("MESSAGE Your move");
|
||||
}
|
||||
|
||||
// Repeatedly get commands from the client and process them.
|
||||
while (true) {
|
||||
String command = input.readLine();
|
||||
if (command.startsWith("MOVE")) {
|
||||
int location = Integer.parseInt(command.substring(5));
|
||||
if (legalMove(location, this)) {
|
||||
output.println("VALID_MOVE");
|
||||
output.println(hasWinner() ? "VICTORY"
|
||||
: boardFilledUp() ? "TIE"
|
||||
: "");
|
||||
} else {
|
||||
output.println("MESSAGE ?");
|
||||
}
|
||||
} else if (command.startsWith("QUIT")) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.out.println("Player died: " + e);
|
||||
} finally {
|
||||
try {socket.close();} catch (IOException e) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
BIN
icons/glasses.png
Normal file
|
After Width: | Height: | Size: 2.9 KiB |
BIN
icons/hammer.png
Normal file
|
After Width: | Height: | Size: 2.2 KiB |
BIN
icons/key.png
Normal file
|
After Width: | Height: | Size: 2.1 KiB |
BIN
icons/keyboard.png
Normal file
|
After Width: | Height: | Size: 1.8 KiB |
BIN
icons/mirror.png
Normal file
|
After Width: | Height: | Size: 4 KiB |
BIN
icons/money.png
Normal file
|
After Width: | Height: | Size: 6.6 KiB |
BIN
icons/nail.png
Normal file
|
After Width: | Height: | Size: 4.4 KiB |
BIN
icons/paper.png
Normal file
|
After Width: | Height: | Size: 2.7 KiB |
BIN
icons/pencil.png
Normal file
|
After Width: | Height: | Size: 2.3 KiB |
BIN
icons/person.png
Normal file
|
After Width: | Height: | Size: 4.3 KiB |
BIN
icons/phone.png
Normal file
|
After Width: | Height: | Size: 7 KiB |
BIN
icons/purse.png
Normal file
|
After Width: | Height: | Size: 2.1 KiB |
BIN
icons/ring.png
Normal file
|
After Width: | Height: | Size: 5.1 KiB |
BIN
icons/screw.png
Normal file
|
After Width: | Height: | Size: 1.8 KiB |
BIN
icons/sign.png
Normal file
|
After Width: | Height: | Size: 4.3 KiB |
BIN
icons/sock.png
Normal file
|
After Width: | Height: | Size: 4.5 KiB |
BIN
icons/sponge.png
Normal file
|
After Width: | Height: | Size: 3.6 KiB |
BIN
icons/spoon.png
Normal file
|
After Width: | Height: | Size: 5 KiB |
BIN
icons/table.png
Normal file
|
After Width: | Height: | Size: 1.9 KiB |
BIN
icons/thread.png
Normal file
|
After Width: | Height: | Size: 8.5 KiB |
BIN
icons/tomato.png
Normal file
|
After Width: | Height: | Size: 3.5 KiB |
BIN
icons/towel.png
Normal file
|
After Width: | Height: | Size: 1.9 KiB |
BIN
icons/toy.png
Normal file
|
After Width: | Height: | Size: 8.1 KiB |
BIN
icons/tree.png
Normal file
|
After Width: | Height: | Size: 8.2 KiB |