Question: My project is to create a tic tac toe game that has a player(the user) and then the computer. Whoever is 'X' always goes first,
My project is to create a tic tac toe game that has a player(the user) and then the computer. Whoever is 'X' always goes first, and the human player gets to choose which marker they want (X or O). I think that my methods are all correct and maybe there is just a problem in the implementation portion in the main method, but I can't seem to get the board displaying properly each time, and I don't think that the game is going back and forth with the turns like it's supposed to. I just need some guidance on where I'm going wrong.
This is my main method:
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package tictactoe;
import java.util.Scanner;
/** * * @author brikr */ public class TicTacToe {
/** * @param args the command line arguments */ public static void main(String[] args) {
char player = ' '; char computerMarker = ' '; int humanCount = 0; int computerCount = 0; Board theBoard = new Board(); char [][] board = new char[3][3];
Scanner input = new Scanner(System.in); System.out.println("What is your name?"); String name = input.nextLine(); System.out.println("Would you like to be X or O?"); char humanMark = input.next().charAt(0); char humanMarker = Character.toUpperCase(humanMark); while(humanMark != 'x' && humanMark != 'o'){ System.out.println("Please pick either 'X' or 'O'"); humanMark = input.next().charAt(0); humanMarker = Character.toUpperCase(humanMark); } if (humanMarker == 'X') { computerMarker = 'O'; } else { computerMarker = 'X'; } System.out.println("What would you like the computer's difficulty level to be? 0 or 1?"); int difficultyLevel = input.nextInt(); while (difficultyLevel != 0 && difficultyLevel != 1) { System.out.println("Please enter a value that is 0 or 1"); difficultyLevel = input.nextInt(); } Player human = new HumanPlayer(name, humanMarker); Player computer = new ComputerPlayer(difficultyLevel, computerMarker); char turn = 'X'; while (theBoard.gameStatus() == 'N') { if (human.getMarker() == turn) { human.move(theBoard); theBoard.setTheBoard(human.move(theBoard), human.getMarker()); theBoard.displayTheBoard(); } else { System.out.println(computer.move(theBoard)); theBoard.setTheBoard(computer.move(theBoard), computer.getMarker()); theBoard.displayTheBoard(); } turn = turn == 'O' ? 'X' : 'O'; System.out.println(theBoard.gameStatus()); } } }
This is my abstract Player class:
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package tictactoe;
/** * * @author brikr */ public abstract class Player { private char marker;
public Player(char marker){ this.marker = marker; }
public char getMarker() { return marker; }
abstract int move(Board theBoard); }
This is my HumanPlayer class:
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package tictactoe;
import java.util.Scanner;
/** * * @author brikr */ public class HumanPlayer extends Player{ Scanner input = new Scanner(System.in); private String name; public HumanPlayer(String name, char marker){ super(marker); this.name = name; }
Board b = new Board(); @Override int move(Board theBoard) { int move = 0; if(super.getMarker() == 'X'){ System.out.println("It's your turn first!"); b.displayTheBoard(); System.out.println("You can move to any number 1-9. What do you choose?"); move = input.nextInt(); b.isValidMove(move); while(b.isValidMove(move) == false){ System.out.println("That move is not valid. Please try again."); move = input.nextInt(); } } else if(super.getMarker() == 'O'){ System.out.println("It's the computer's turn first!"); } return move; } }
This is my ComputerPlayer class:
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package tictactoe;
/** * * @author brikr */ public class ComputerPlayer extends Player { private int difficultyLevel; public ComputerPlayer(int difficultyLevel, char marker){ super(marker); this.difficultyLevel = difficultyLevel; } Board b = new Board(); @Override int move(Board theBoard) { int move = (int)(Math.random()*9); b.isValidMove(move); while(b.isValidMove(move) == false){ move = (int)(Math.random() * 9); } if(b.isValidMove(move) == true){ //b.displayTheBoard(); } return move; } }
This is my Board class:
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package tictactoe;
/** * * @author brikr */ public class Board {
private final char[][] theBoard = new char[3][3]; public Board() { this.theBoard[0][0] = '1'; this.theBoard[0][1] = '2'; this.theBoard[0][2] = '3'; this.theBoard[1][0] = '4'; this.theBoard[1][1] = '5'; this.theBoard[1][2] = '6'; this.theBoard[2][0] = '7'; this.theBoard[2][1] = '8'; this.theBoard[2][2] = '9'; }
public void setTheBoard(int move, char character){ if(move == 1){ theBoard[0][0] = character; } if(move == 2){ theBoard[0][1] = character; } if(move == 3){ theBoard[0][2] = character; } if(move == 4){ theBoard[1][0] = character; } if(move == 5){ theBoard[1][1] = character; } if(move == 6){ theBoard[1][2] = character; } if(move == 7){ theBoard[2][0] = character; } if(move == 8){ theBoard[2][1] = character; } if(move == 9){ theBoard[2][2] = character; } } boolean isValidMove(int attemptedMove) { if (attemptedMove == 1) { if (theBoard[0][0] == 'X' || theBoard[0][0] == 'O') { return false; } } if (attemptedMove == 2) { if (theBoard[0][1] == 'X' || theBoard[0][1] == 'O') { return false; } } if (attemptedMove == 3) { if (theBoard[0][2] == 'X' || theBoard[0][2] == 'O') { return false; } } if (attemptedMove == 4) { if (theBoard[1][0] == 'X' || theBoard[1][0] == 'O') { return false; } } if (attemptedMove == 5) { if (theBoard[1][1] == 'X' || theBoard[1][1] == 'O') { return false; } } if (attemptedMove == 6) { if (theBoard[1][2] == 'X' || theBoard[1][2] == 'O') { return false; } } if (attemptedMove == 7) { if (theBoard[2][0] == 'X' || theBoard[2][0] == 'O') { return false; } } if (attemptedMove == 8) { if (theBoard[2][1] == 'X' || theBoard[2][1] == 'O') { return false; } } if (attemptedMove == 9) { if (theBoard[2][2] == 'X' || theBoard[2][2] == 'O') { return false; } } if (attemptedMove > 9) { return false; } else if (attemptedMove < 1) { return false; }
return true; }
public char newTurn(char player) { return player == 'O' ? 'X' : 'O'; }
public void displayTheBoard() { System.out.println(theBoard[0][0] + " | " + theBoard[0][1] + " | " + theBoard[0][2] + " | "); System.out.println(("-----------")); System.out.println(theBoard[1][0] + " | " + theBoard[1][1] + " | " + theBoard[1][2] + " | "); System.out.println(("-----------")); System.out.println(theBoard[2][0] + " | " + theBoard[2][1] + " | " + theBoard[2][2] + " | "); }
public char gameStatus() { if ((theBoard[0][0] == theBoard[0][1]) && (theBoard[0][1] == theBoard[0][2])) { return theBoard[0][0]; } else if ((theBoard[1][0] == theBoard[1][1]) && (theBoard[1][1] == theBoard[1][2])) { return theBoard[1][0]; } else if ((theBoard[2][0] == theBoard[2][1]) && (theBoard[2][1] == theBoard[2][2])) { return theBoard[2][0]; } else if ((theBoard[0][0] == theBoard[1][1]) && (theBoard[1][1] == theBoard[2][2])) { return theBoard[0][0]; } else if ((theBoard[0][2] == theBoard[1][1]) && (theBoard[1][1] == theBoard[2][0])) { return theBoard[1][2]; } else if ((theBoard[0][0] == theBoard[1][0]) && (theBoard[1][0] == theBoard[2][0])) { return theBoard[0][0]; } else if ((theBoard[0][1] == theBoard[1][1]) && (theBoard[1][1] == theBoard[2][1])) { return theBoard[0][1]; } else if ((theBoard[0][2] == theBoard[1][2]) && (theBoard[1][2] == theBoard[2][2])) { return theBoard[0][2]; } for (int row = 0; row < 3; row++) { for (int column = 0; column < 3; column++) { if (theBoard[row][column] == ' ') { return 'T'; } } } return 'N'; }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
