Here\'s my server client, import java.io.*; import java.net.*; import java.util.
ID: 3624170 • Letter: H
Question
Here's my server client,import java.io.*;
import java.net.*;
import java.util.*;
class TCPClient {
public static void main(String argv[]) throws Exception {
public int square[9] = 0;
public int ClientMove;
public int endgame = 0;
Socket clientSocket = new Socket(10.0.65.10, 5555);
while (endgame==0){
PlayerMove(square[]);
SendToServer();
PrintGrid(square[]);
ServerMove(square[]);
PrintGrid(square[]);
CheckStatus(square[]);
}
clientSocket.close();
}
}
//print grid
public void PrintGrid(square[]){
System.out.println(square[1] + " | " + square[2] + " | " + square[3]);
System.out.println(--|---|--);
System.out.println(square[4] + " | " + square[5] + " | " + square[6]);
System.out.println(--|---|--);
System.out.println(square[7] + " | " + square[8] + " | " + square[9]);
}
public void ClientMove(square[]){
System.out.println("Your move. Please enter a valid number between [1-9]");//user inputs data
ClientMove = args[0];
if ( Clientmove > 9 || Clientmove < 1){
System.out.println("Invalid move. try again");
ClientMove = args[0];
}
else{
square[ClientMove]= 1;
}
}
public void SendToServer(){
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeBytes(ClientMove.toString() + ' ');
}
public void ServerMove(square[]){
int ServerMove = integer.parseInt(inFromServer.readLine());
square[ServerMove]=2; //update client's version of array
}
//check for winner
public int CheckStatus(square[]){
if (sqaure[1]==1 && sqaure[5]==1 && sqaure[9]==1 || sqaure[3]==1 && sqaure[5]==1 && sqaure[7]==1 //diagonals
sqaure[1]==1 && sqaure[4]==1 && sqaure[7]==1 || sqaure[2]==1 && sqaure[5]==1 && sqaure[8]==1
|| sqaure[3]==1 && sqaure[6]==1 && sqaure[9]==1 /*columns*/ sqaure[1]==1 && sqaure[2]==1 && sqaure[3]==1
|| sqaure[4]==1 && sqaure[5]==1 && sqaure[6]==1
|| sqaure[7]==1 && sqaure[8]==1 && sqaure[9]==1 /*rows*/)
{
System.out.println("You beat the server. Way to go!");
endgame = 1;
}
else if (sqaure[1]==2 && sqaure[5]==2 && sqaure[9]==2 || sqaure[3]==2 && sqaure[5]==2 && sqaure[7]==2 //diagonals
sqaure[1]==2 && sqaure[4]==2 && sqaure[7]==2 || sqaure[2]==2 && sqaure[5]==2 && sqaure[8]==2
|| sqaure[3]==2 && sqaure[6]==2 && sqaure[9]==2 /*columns*/ sqaure[1]==2 && sqaure[2]==2 && sqaure[3]==2
|| sqaure[4]==2 && sqaure[5]==2 && sqaure[6]==2
|| sqaure[7]==1 && sqaure[8]==1 && sqaure[9]==1 /*rows*/)
{
System.out.println("Sorry, you lose!");
endgame = 1;
}
else if ((sqaure[1]>0 && sqaure[2]>0 && sqaure[3]>0 && sqaure[4]>0 && sqaure[5]>0 && sqaure[6]>0
&& sqaure[7]>0 && sqaure[8]>0 && sqaure[9]>0) //check for a draw, all locations > 0 and no winner
{
System.out.println("It's a draw");
endgame = 1;
}
}
thanks guys
Explanation / Answer
Note that array indexing starts at 0. Fixed.
import java.io.*;
import java.net.*;
import java.util.*;
public class TCPClient {
private static int[] square = new int[9];
private static int endgame = 0;
private static DataOutputStream outToServer;
private static DataInputStream inFromServer;
private static Scanner kb;
public static void main(String argv[]) throws IOException {
kb = new Scanner(System.in);
//Socket clientSocket = new Socket("10.0.65.10", 5555);
Socket clientSocket = new Socket("0.0.0.0", 5555);
outToServer = new DataOutputStream(clientSocket.getOutputStream());
inFromServer = new DataInputStream(clientSocket.getInputStream());
PrintGrid(square);
while (endgame==0){
ClientMove(square);
ServerMove(square);
PrintGrid(square);
CheckStatus(square);
}
clientSocket.close();
}
//print grid
public static void PrintGrid(int[] square){
System.out.println("-------------");
System.out.println("| "+getChar(square[0]) + " | " + getChar(square[1]) + " | " + getChar(square[2])+" |");
System.out.println("|---|---|---|");
System.out.println("| "+getChar(square[3]) + " | " + getChar(square[4]) + " | " + getChar(square[5])+" |");
System.out.println("|---|---|---|");
System.out.println("| "+getChar(square[6]) + " | " + getChar(square[7]) + " | " + getChar(square[8])+" |");
System.out.println("-------------");
}
private static char getChar(int s)
{
switch(s)
{
case 1: return 'O';
case 2: return 'X';
default: return ' ';
}
}
public static void ClientMove(int[] square) throws IOException {
System.out.println("Your move. Please enter a valid number between [1-9]");//user inputs data
int ClientMove =kb.nextInt();
while ( ClientMove > 9 || ClientMove < 1){
System.out.println("Invalid move. try again");
ClientMove = kb.nextInt();
}
ClientMove--;
square[ClientMove]= 1;
SendToServer(ClientMove);
if(CheckStatus(square)==1)
{
System.exit(1);
}
}
public static void SendToServer(int ClientMove) throws IOException {
outToServer.writeInt(ClientMove);
}
public static void ServerMove(int[] square) throws IOException {
int ServerMove = inFromServer.readInt();
square[ServerMove]=2; //update client's version of array
}
//check for winner
public static int CheckStatus(int[] square){
if (square[0]==1 && square[4]==1 && square[7]==1 || square[2]==1 && square[4]==1 && square[6]==1 || /*diagonals */
square[0]==1 && square[4]==1 && square[8]==1 || square[2]==1 && square[4]==1 && square[6]==1
|| square[2]==1 && square[5]==1 && square[8]==1 || /*columns*/ square[0]==1 && square[1]==1 && square[2]==1
|| square[3]==1 && square[4]==1 && square[5]==1
|| square[6]==1 && square[7]==1 && square[8]==1 /*rows*/)
{
System.out.println("You beat the server. Way to go!");
endgame = 1;
}
else if (square[1]==2 && square[5]==2 && square[9]==2 || square[3]==2 && square[5]==2 && square[7]==2 || /*diagonals */
square[0]==2 && square[4]==2 && square[8]==2 || square[2]==2 && square[4]==2 && square[6]==2
|| square[3]==2 && square[6]==2 && square[9]==2 || /*columns*/ square[1]==2 && square[2]==2 && square[3]==2
|| square[4]==2 && square[5]==2 && square[6]==2
|| square[7]==1 && square[8]==1 && square[9]==1 /*rows*/)
{
System.out.println("Sorry, you lose!");
endgame = 1;
}
else if (square[0]>0 && square[1]>0 && square[2]>0 && square[3]>0 && square[4]>0 && square[5]>0
&& square[6]>0 && square[7]>0 && square[8]>0) //check for a draw, all locations > 0 and no winner
{
System.out.println("It's a draw");
endgame = 1;
}
return endgame;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.