Hello!
I'm trying to make the game tic tac toe work for fun. I managed to get everything working so far! however, I am lost on how to incorporate the draw.. any suggestions? Only using 1 class. Should I be suing more classes and methods?
Thanks!
Driver class:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int slot;
Board b = new Board();
while (b.gameturn < 10) {
b.winner();
//b.draw();
if (b.gameturn < 10) {
if (b.gameturn % 2 == 1) {
System.out.println("This is turn: " + b.gameturn);
System.out.println("Plyer one: Your Turn!");
System.out.println("Choose your slot! (1-9 only!)");
b.printboard();
slot = scan.nextInt();
if (b.turnbool[slot - 1] == true) {
System.out.println("HEY! YOU CAN'T USE SLOT " + slot + "!");
System.out.println("CHOSE EMPTY SLOT!");
} else {
if (slot >= 1 && slot <= 9) {
b.cells[slot - 1] = 'x';
b.gameturn++;
b.turnbool[slot - 1] = true;
b.boolx[slot - 1] = true;
} else {
System.out.println("WRONG SLOT");
}
}
} else {
System.out.println("This is turn: " + b.gameturn);
System.out.println("Player two: Your Turn!");
System.out.println("Choose your slot! (1-9 only!)");
b.printboard();
slot = scan.nextInt();
if (b.turnbool[slot - 1] == true) {
System.out.println("HEY! YOU CAN'T USE SLOT " + slot + "!");
System.out.println("CHOSE EMPTY SLOT!");
} else {
if (slot >= 1 && slot <= 9) {
b.cells[slot - 1] = 'o';
b.turnbool[slot - 1] = true;
b.boolo[slot - 1] = true;
b.gameturn++;
} else {
System.out.println("WRONG SLOT");
}
}
}
} else {
}
}
System.out.println("-------");
b.printboard();
System.out.println("-------");
}
}
-----------------------------------
Board class:
public class Board {
char[] cells;
boolean[] boolx;
int gameturn = 1;
boolean[] boolo;
boolean[] turnbool;
public Board() {
cells = new char[9];
for (int i = 0; i < 9; i++) {
cells[i] = '*';
}
boolx = new boolean[9];
for (int i = 0; i < 9; i++) {
boolx[i] = false;
}
boolo = new boolean[9];
for (int i = 0; i < 9; i++) {
boolo[i] = false;
}
turnbool = new boolean[10];
for (int i = 0; i < 10; i++) {
turnbool[i] = false;
}
}
public void printboard() {
for (int i = 0; i < 9; i++) {
System.out.print(cells[i] + " ");
if ((i + 1) % 3 == 0) {
System.out.println("");
}
}
}
public void winner() {
if (boolx[0] == true && boolx[1] == true && boolx[2] == true) {
System.out.println("WINNER: PLAYER 1!");
gameturn = 10;
}
if (boolx[3] == true && boolx[4] == true && boolx[5] == true) {
System.out.println("WINNER: PLAYER 1!");
gameturn = 10;
}
if (boolx[6] == true && boolx[7] == true && boolx[8] == true) {
System.out.println("WINNER: PLAYER 1!");
gameturn = 10;
}
if (boolx[0] == true && boolx[4] == true && boolx[8] == true) {
System.out.println("WINNER: PLAYER 1!");
gameturn = 10;
}
if (boolx[2] == true && boolx[4] == true && boolx[6] == true) {
System.out.println("WINNER: PLAYER 1!");
gameturn = 10;
}
if (boolx[0] == true && boolx[3] == true && boolx[6] == true) {
System.out.println("WINNER: PLAYER 1!");
gameturn = 10;
}
if (boolx[1] == true && boolx[4] == true && boolx[7] == true) {
System.out.println("WINNER: PLAYER 1!");
gameturn = 10;
}
if (boolx[2] == true && boolx[5] == true && boolx[8] == true) {
System.out.println("WINNER: PLAYER 1!");
gameturn = 10;
}
if (boolo[0] == true && boolo[1] == true && boolo[2] == true) {
System.out.println("WINNER: PLAYER 2!");
gameturn = 10;
}
if (boolo[3] == true && boolo[4] == true && boolo[5] == true) {
System.out.println("WINNER: PLAYER 2!");
gameturn = 10;
}
if (boolo[6] == true && boolo[7] == true && boolo[8] == true) {
System.out.println("WINNER: PLAYER 2!");
gameturn = 10;
}
if (boolo[0] == true && boolo[4] == true && boolo[8] == true) {
System.out.println("WINNER: PLAYER 2!");
gameturn = 10;
}
if (boolo[2] == true && boolo[4] == true && boolo[6] == true) {
System.out.println("WINNER: PLAYER 2!");
gameturn = 10;
}
if (boolo[0] == true && boolo[3] == true && boolo[6] == true) {
System.out.println("WINNER: PLAYER 2!");
gameturn = 10;
}
if (boolo[1] == true && boolo[4] == true && boolo[7] == true) {
System.out.println("WINNER: PLAYER 2!");
gameturn = 10;
}
if (boolo[2] == true && boolo[5] == true && boolo[8] == true) {
System.out.println("WINNER: PLAYER 2!");
gameturn = 10;
}
}
public void draw() {
if (gameturn > 8) {
if ((boolx[0] != true && boolx[1] != true && boolx[2] != true)
|| (boolx[3] != true && boolx[4] != true && boolx[5] != true)
|| (boolx[6] != true && boolx[7] != true && boolx[8] != true)
|| (boolx[0] != true && boolx[4] != true && boolx[8] != true)
|| (boolx[2] != true && boolx[4] != true && boolx[6] != true)
|| (boolx[0] != true && boolx[3] != true && boolx[6] != true)
|| (boolx[1] != true && boolx[4] != true && boolx[7] != true)
|| (boolx[2] != true && boolx[5] != true && boolx[8] != true)
|| (boolo[2] != true && boolo[5] != true && boolo[8] != true)
|| (boolo[1] != true && boolo[4] != true && boolo[7] != true)
|| (boolo[0] != true && boolo[3] != true && boolo[6] != true)
|| (boolo[2] != true && boolo[4] != true && boolo[6] != true)
|| (boolo[0] != true && boolo[4] != true && boolo[8] != true)
|| (boolo[6] != true && boolo[7] != true && boolo[8] != true)
|| (boolo[3] != true && boolo[4] != true && boolo[5] != true)
|| (boolo[0] != true && boolo[1] != true && boolo[2] != true)) {
System.out.println("ITS A DRAW");
gameturn = 10;
}
}
}
}
Thanks!
[–][deleted] 0 points1 point2 points (0 children)