OK so I have it working. I know its buggy, and theres a few holes to fill u can legitly play. Now I just want to make it so I can have a Client/Server type game. So heres the TicTacToe Class
import java.util.Scanner;
import java.util.Random;
public class TicTacToe {
public static void main(String[] args) {
Scanner keyboard=new Scanner(System.in);
String[][] board= {{" "," "," "},{" "," "," "},{" "," "," "}};
int x=0;
int y=0;
int option=0;
String player1;
String player2;
System.out.println("Press 1 for 2 players, or press 7 to exit");
option=keyboard.nextInt();
if (option==1) {
System.out.println("Player 1 enter your name");
player1=keyboard.next();
System.out.println("Player 2 enter your name");
player2=keyboard.next();
boolean w=winner(board);
while (w==true||w==false) {
System.out.println(player1+"Enter your choice");
x=keyboard.nextInt();
y=keyboard.nextInt();
if (board[x][y]=="X") {
System.out.println("That space is already taken");
continue;
}
if (board[x][y]=="Y"){
System.out.println("That space is already taken");
continue;
}
board[x][y]="X";
displayBoard(board);
do {
System.out.println(player2+" enter your choice");
x=keyboard.nextInt();
y=keyboard.nextInt();
if (board[x][y]=="X") {
System.out.println("That space is already taken");
continue;
}
if (board[x][y]=="O") {
System.out.println("That space is already taken");
continue;
}
board[x][y]="O";
displayBoard(board);
break;
}
while (true);
}
}
}
public static void displayBoard (String z[][]) {
System.out.println(" "+z[0][0]+" | "+z[0][1]+" | "+z[0][2]);
System.out.println(" ---------");
System.out.println(" "+z[1][0]+" | "+z[1][1]+" | "+z[1][2]);
System.out.println(" ---------");
System.out.println(" "+z[2][0]+" | "+z[2][1]+" | "+z[2][2]);
}
public static boolean winner (String z[][])
{
if ( ( z[0][0]=="X" && z[0][1]=="X" && z[0][2]=="X" )
|| ( z[1][0]=="X" && z[1][1]=="X" && z[1][2]=="X" )
|| ( z[2][0]=="X" && z[2][1]=="X" && z[2][2]=="X" )
|| ( z[0][0]=="X" && z[1][0]=="X" && z[2][0]=="X" )
|| ( z[0][1]=="X" && z[1][1]=="X" && z[2][1]=="X" )
|| ( z[0][0]=="X" && z[1][1]=="X" && z[2][2]=="X" )
|| ( z[0][2]=="X" && z[1][1]=="X" && z[2][0]=="X" ) )
{
return true; }
else {return false;}
}
}
and I need to make it work with these other classes...
import java.net.*;
import java.io.*;
public class TicTacToeProtocol {
private static final int WAITING = 0;
private static final int SENTHELLO = 1;
private int state = WAITING;
public String processInput(String theInput) {
String theOutput = null;
if (state == WAITING) {
theOutput = "Hello from Server";
state = SENTHELLO;
}
else { // state is SENTHELLO
try {
// Display message from client.
System.out.println("Client: " + theInput);
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
// Display prompt for server.
System.out.print(">> ");
// Read a line of input from the server.
theOutput = stdIn.readLine();
} catch (IOException e) {
System.err.println("IMProtocol: IO error.");
System.exit(1);
} // end catch
} // end else
return theOutput;
} // end processInput
} // end IMProtocol
import java.net.*;
import java.io.*;
public class TicTacToeServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
boolean listening = true;
try {
serverSocket = new ServerSocket(4444);
} catch (IOException e) {
System.err.println("Could not listen on port: 4444.");
System.exit(-1);
}
while (listening)
new TicTacToeMultiServerThread(serverSocket.accept()).start();
serverSocket.close();
}
}
import java.io.*;
import java.net.*;
public class TicTacToeClient {
public static void main(String[] args) throws IOException {
Socket TicTacToeSocket = null;
PrintWriter out = null;
//BufferedReader in = null;
Message inMessage = null; //
ObjectInputStream in = null; //
try {
TicTacToeSocket = new Socket("192.168.1.104", 4444);
out = new PrintWriter(TicTacToeSocket.getOutputStream(), true);
//in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
in = new ObjectInputStream(TicTacToeSocket.getInputStream()); //
inMessage = (Message) in.readObject(); //
} catch (UnknownHostException e) {
System.err.println("Don't know about host: ");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: 192.168.0.11.");
System.exit(1);
}
catch (ClassNotFoundException cnfe) { //
System.err.println("Problem reading object: class not found"); //
System.exit(1); //
} //
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String fromServer;
String fromUser;
//while ((fromServer = in.readLine()) != null) {
while ((fromServer = inMessage.getResponse()) != null) {
System.out.println("Server: " + fromServer);
if (fromServer.equals("Bye"))
break;
// Display prompt for client.
System.out.print("> ");
// Read a line of input from the client.
fromUser = stdIn.readLine();
// Echo client's input.
if (fromUser != null) {
System.out.println("Client: " + fromUser);
out.println(fromUser);
}
// Get the next message from the server.
try { //
inMessage = (Message) in.readObject(); //
} //
catch (ClassNotFoundException cnfe) { //
System.err.println("IMClient: Problem reading object: class not found"); //
System.exit(1); //
} //
}
out.close();
in.close();
stdIn.close();
TicTacToeSocket.close();
}
}
public class Message implements java.io.Serializable
{
// instance variables - replace the example below with your own
private String response;
/**
* Constructor for objects of class Message
*/
public Message(String r)
{
// initialize instance variables
this.response = r;
}
/**
* getResponse: getter method for response, an instance variable
*
* @param none
* @return a String, the response
*/
public String getResponse()
{
return response;
}
}
import java.net.*;
import java.io.*;
public class TicTacToeMultiServerThread extends Thread {
private Socket socket = null;
public TicTacToeMultiServerThread(Socket socket) {
super("IMMultiServerThread");
this.socket = socket;
}
public void run() {
//System.out.println("Inside run.");
try {
//PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream()); //
BufferedReader in = new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
String inputLine, outputLine;
TicTacToeProtocol kkp = new TicTacToeProtocol();
outputLine = kkp.processInput(null);
//out.println(outputLine);
Message message = new Message(outputLine); //
out.writeObject(message); //
while ((inputLine = in.readLine()) != null) {
outputLine = kkp.processInput(inputLine);
//out.println(outputLine);
message = new Message(outputLine); //
out.writeObject(message); //
if (outputLine.equals("Bye"))
break;
}
System.out.println("Close connection");
out.close();
in.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
I dont believe I have to do that much with the Server class, but all the other classes I would appreciate if someone were able to tell me where to start, and Ill prolly be able to pick up the rest