- #1
Albeaver89
- 18
- 0
Homework Statement
So, we have a project to make a tic-tac-toe game and for bonus points we can make sure the player(s) can not enter the same position more than once. I can't think of a way to do this. I'm sure I need to use a 'for loop' or a 'while loop'.
I am using eclipse to write the code. Here is the constructs and methods:
"
public class TicTacToe {
static String P1;
static String P2;
static String P3;
static String P4;
static String P5;
static String P6;
static String P7;
static String P8;
static String P9;
public TicTacToe(){
P1 = "1";
P2 = "2";
P3 = "3";
P4 = "4";
P5 = "5";
P6 = "6";
P7 = "7";
P8 = "8";
P9 = "9";
}
public void drawBoard(){
System.out.println(P1 + "|" + P2 + "|" + P3 + "|");
System.out.println(P4 + "|" + P5 + "|" + P6 + "|");
System.out.println(P7 + "|" + P8 + "|" + P9 + "|");
}
public boolean checkWin(String player)
{
if (P1.equals(player) && P2.equals(player) && P3.equals(player))
return true;
else if (P4.equals(player) && P5.equals(player) && P6.equals(player))
return true;
else if(P7.equals(player) && P8.equals(player) && P9.equals(player))
return true;
else if(P7.equals(player) && P8.equals(player) && P9.equals(player))
return true;
else if (P1.equals(player) && P5.equals(player) && P9.equals(player))
return true;
else if(P3.equals(player) && P5.equals(player) && P7.equals(player))
return true;
else if(P1.equals(player) && P4.equals(player) && P7.equals(player))
return true;
else if(P2.equals(player) && P5.equals(player) && P8.equals(player))
return true;
else if(P3.equals(player) && P6.equals(player) && P9.equals(player))
return true;
else return false;
}
public void addX(int Position)
{
if(Position == 1)
P1 = "X";
else if(Position == 2)
P2 = "X";
else if(Position ==3)
P3 = "X";
else if(Position == 4)
P4 = "X";
else if(Position == 5)
P5 = "X";
else if(Position == 6)
P6 = "X";
else if(Position == 7)
P7 = "X";
else if(Position == 8)
P8 = "X";
else if(Position == 9)
P9 = "X";
else System.out.println("Please give a number 1-9");
}
public void addO(int Position)
{
if(Position == 1)
P1 = "O";
else if(Position == 2)
P2 = "O";
else if(Position ==3)
P3 = "O";
else if(Position == 4)
P4 = "O";
else if(Position == 5)
P5 = "O";
else if(Position == 6)
P6 = "O";
else if(Position == 7)
P7 = "O";
else if(Position == 8)
P8 = "O";
else if(Position == 9)
P9 = "O";
else System.out.println("Please give a number 1-9");
}
}
"
And here is the 'tester' or the part where it runs:
"import java.util.Scanner;
public class tictactoetester1 {
private static Scanner in;
public static void main(String[] args) {
TicTacToe game = new TicTacToe();
boolean gameOver = false;
in = new Scanner(System.in);
String turn = "X";
game.drawBoard();
for (int i=0; i<9 && !gameOver ; i++)
{
if(turn == "X")
{
System.out.println("Please type a 1-9, corresponding to the game board");
in.hasNextDouble();
int input = (int) in.nextDouble();
game.addX(input);
game.checkWin("X");
game.drawBoard();
if(game.checkWin("X") == true){
gameOver = true;
System.out.println("X Wins!");
}
turn = "O";
}
else
if(turn == "O")
{
System.out.println("Please type a 1-9, corresponding to the game board");
in.hasNextDouble();
int inputO = (int) in.nextDouble(); ;
game.addO(inputO);
game.checkWin("O");
game.drawBoard();
if(game.checkWin("O") == true){
gameOver = true;
System.out.println("O Wins!");
}
turn = "X";
}
}
System.out.println("Game Over");
}
}
"
Any help will be greatly appreciated. Also if something in my code seems really off just tell me. I take criticism well...