In Java Please Part A Write a class Player which has instance variables playerNa
ID: 673672 • Letter: I
Question
In Java Please
Part A
Write a class Player which has instance variables playerName, a String, the name of the player symbol, a String, the symbol the player will use on a tic-tac-toe grid. (e.g. "X", "O") You may choose to use a char instead. board, a TicTac (we will be creating this class in part B) wins, an int to keep track of how many times a player has won
In the mutator for symbol, check that the String given has length exactly 1 and is not " " (the String consisting of a single space).
In the toString, use the player's name and put the symbol in parens, something like
Test out your class in a harness.
Part B
Add a class TicTac which has instance variables
grid, a 2D array of Strings (or chars if you prefer)
player1 and player2, both Players
movesLeft, an int, which keeps track of how many moves are left in the game.
In the mutators for the players, only accept the new value if the Player passed in is not null, and make sure to set the players' boards to this TicTac and their symbols to "X" for player1 or "O" for player2
Add a method resetGrid which sets up grid up as a 3x3 array and fills all elements with " " (the string consisting of a single space). Also set movesLeft to 9.
In the default constructor, use resetGrid to set up the grid, and set up player1 and player2.
For testing purposes, write accessor and mutator for grid so that we can pass 2D string arrays in and out to set grid.
EC+10: In the mutator, only accept arrays that are 3x3 and contain only " ", and "X" and "O".
Add a method makemove( which takes as arguments two ints and a Player, and returns a boolean. If the ints are within the bounds of grid, check that element of grid. If it is currently " " put the Player's symbol at that location in grid, decrease movesLeft, and return true. Otherwise make no changes to and return false.
Add a toString that returns a string for the grid in a nice format. e.g.
In Harness, in the main, test that your TicTac methods so far work.
Although you may, you don't have to make this class work for boards of sizes other than 3x3.
Part C
In TicTac, add a method ticTacWin() which returns player1 if player1's symbol has a tic-tac-toe win in any row, column, or diagonal of grid, player1 if player2's symbol has a win, and null if neither has a win.
In the case that both have a win, you can return either (i.e., whichever you find first). This should never come up in a real game anyway.
There are many ways to do this, but a method that checks each row and column individually, rather than using loops, will not receive full credit (hint: a single rather than nested loop is usually the easiest approach for rows and columns). However, checking diagonals individually is reasonable.
In Harness, in main, create at least three 3 x 3 arrays of strings (you may do this however you please), and fill them with " ", "X", and "O" so that one has a diagonal win for X, one has a vertical win for O, and one has at least three X's and three O's, but neither wins. Create a TicTac object, set its grid to each of your examples, and check for each that ticTacWin() correctly reports the winner.
Part D
In Player, add a method userMove which interacts with the user. It shows (by printing) the player the current state of the board (remember, Player has an instance var for the board) and then prompts (see input incantations above) the player by name for a row and a column number for the player's next move.
The player will enter a number from 1 to 3 for each; but remember that the board uses numbers from 0 to 2, so you will need to translate. Try to have the board use makemove to set that position to the player's symbol. As long as makemove returns false (could not make that move) prompt the user again.
In TicTac, add a method playGame. In playGame reset the grid, then repeatedly have one player do a move, then switch to the other player, until ticTacWin returns a winner (not null) or there are no movesLeft. Print a congratulatory message to the winner if there is one, and show how many times they have won. Otherwise announce a tie.
Part E
Add a new class TicTacInterface with a main. In the main, set up a TicTac.
Prompt the first player for their name, set up a Player with the name, and have this Player join the TicTac as player1. Then do the same for a second player for player2.
Once the players are set up, in a loop call playGame for the ticTac, then ask whether they want to quit or play again.
EC +10
To TicTac, add an array of Strings winMessages and in the constructor fill it with messages of congratulation for winners, e.g. "Good Job" "Yay You!" "Your Tic Tac Toe Skillz are Beyond 9000", "I CANNOT WITH ALL THE WINNING OF YOU!" etc. Include at least 10 such messages. When a player wins, randomly choose a message from the list to print.
EC+10
When a player wins, as a reward offer them the opportunity to change their symbol. If they want to change, get the new symbol from them, but don't allow it to be set to the other player's current symbol (keep asking until you get one you can use). No need to care about the case where they set it back to the same thing they had before.
Explanation / Answer
// Player.java
import java.util.Scanner;
public class Player{
String playerName;
String symbol;
TicTac board;
int wins;
void setSymbol(String s){
if(s.length() != 1 || s.equals(" ")){
System.out.println("Invalid value");
}
else symbol = s;
}
public String toString(){
return playerName + "(" + symbol + ")";
}
void userMove(){
while(true){
int r, c;
Scanner in = new Scanner(System.in);
System.out.println("Enter the row number and column number: ");
r = in.nextInt();
c = in.nextInt();
r--;
c--;
if(board.makemove(r, c, this) == true) break;
else System.out.println("Invalid move");
}
}
}
// TicTac.java
public class TicTac{
String[][] grid;
Player player1, player2;
int movesLeft;
void setPlayer1(Player p1){
if(p1 != null){
player1 = p1;
p1.symbol = "X";
}
player1.board = this;
}
void setPlayer2(Player p2){
if(p2 != null){
player2 = p2;
p2.symbol = "O";
}
player2.board = this;
}
void resetGrid(){
grid = new String[3][3];
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
grid[i][j] = " ";
}
}
movesLeft = 9;
}
String[][] getGrid(){
return grid;
}
void setGrid(String g[][]){
if(g.length == 3 && g[0].length == 3 && g[1].length == 3 && g[2].length == 3){
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
if(grid[i][j] != " " && grid[i][j] != "X" && grid[i][j] != "O") return;
}
}
grid = g;
}
}
boolean makemove(int x, int y, Player p){
if(x < 3 && y < 3){
if(grid[x][y] == " "){
grid[x][y] = p.symbol;
movesLeft--;
return true;
}
}
return false;
}
public String toString(){
String str = " 1 2 3";
for(int i = 0; i < 3; i++){
str += " -------------";
str += (i + 1) + " |";
for(int j = 0; j < 3; j++){
str += grid[i][j] + "|";
}
}
str += " -------------";
return str;
}
Player ticTacWin(){
if(grid[0][0] == grid[0][1] && grid[0][0] == grid[0][2]){
if(grid[0][0] == player1.symbol){
player1.wins++;
return player1;
}
else if(grid[0][0] == player2.symbol){
player2.wins++;
return player2;
}
}
if(grid[1][0] == grid[1][1] && grid[1][0] == grid[1][2]){
if(grid[1][0] == player1.symbol){
player1.wins++;
return player1;
}
else if(grid[1][0] == player2.symbol){
player2.wins++;
return player2;
}
}
if(grid[2][0] == grid[2][1] && grid[2][0] == grid[2][2]){
if(grid[2][0] == player1.symbol){
player1.wins++;
return player1;
}
else if(grid[2][0] == player2.symbol){
player1.wins++;
return player2;
}
}
if(grid[0][0] == grid[1][0] && grid[1][0] == grid[2][0]){
if(grid[0][0] == player1.symbol){
player1.wins++;
return player1;
}
else if(grid[0][0] == player2.symbol){
player2.wins++;
return player2;
}
}
if(grid[0][1] == grid[1][1] && grid[1][1] == grid[2][1]){
if(grid[0][1] == player1.symbol){
player1.wins++;
return player1;
}
else if(grid[0][1] == player2.symbol){
player2.wins++;
return player2;
}
}
if(grid[0][2] == grid[1][2] && grid[1][2] == grid[2][2]){
if(grid[0][2] == player1.symbol){
player1.wins++;
return player1;
}
else if(grid[0][2] == player2.symbol){
player2.wins++;
return player2;
}
}
if(grid[0][0] == grid[1][1] && grid[1][1] == grid[2][2]){
if(grid[0][0] == player1.symbol){
player1.wins++;
return player1;
}
else if(grid[0][0] == player2.symbol){
player2.wins++;
return player2;
}
}
if(grid[0][2] == grid[1][1] && grid[1][1] == grid[2][0]){
if(grid[0][2] == player1.symbol){
player1.wins++;
return player1;
}
else if(grid[0][2] == player2.symbol){
player2.wins++;
return player2;
}
}
return null;
}
void playGame(){
resetGrid();
do{
player1.userMove();
player2.userMove();
}while(ticTacWin() != null && movesLeft == 0);
if(ticTacWin() != null){
System.out.println(ticTacWin() + " wins");
System.out.println("You have won " + player1.wins + " times");
}
else{
System.out.println("Its a tie");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.