This is my code for a TicTacToe game. However, I don't know how to write JUnit tests. Please help me with my JUnit Tests. I will post below what I have so far.
package cs145TicTacToe;
import java.util.Scanner;
/**
* A multiplayer Tic Tac Toe game
*/
public class TicTacToe {
private char currentPlayer;
private char[][] board;
private char winner;
/**
* Default constructor
* Initializes the board to be 3 by 3 and to have
spaces
* Initializes the current player to an X
* Initializes the winner to be empty
*/
public TicTacToe() {
board = new char [3][3];
currentPlayer = 'X';
winner = ' ';
for(int row = 0; row < 3; row++)
{
for(int col = 0;
col < 3; col++) {
board[row][col] = ' ';
}
}
}
/**
* Returns if the currentPlayer is an X or an O
* @return the currentPlayer
*/
public char getCurrentPlayer() {
return currentPlayer;
}
/**
* Returns who the winner is
* @return the winner
*/
public char getWinner() {
return winner;
}
/**
* Determines who's turn it is and if the space they
picked is available on the board or not
* @param row represents the row of the board
* @param col represents the column of the board
* @return true if the selected square is available,
false if it isn't
*/
public boolean takeTurn(int row, int col)
{
if(board[row][col] == ' ') {
board[row][col]
= currentPlayer;
checkForWinner();
if(checkForWinner() == true) {
System.out.println(toString());
System.out.println(getCurrentPlayer() + " is the
winner!");
}
//enter if for
check for winner == true
else
if(currentPlayer == 'X') {
currentPlayer = 'O';
}
else {
currentPlayer = 'X';
}
return
true;
}
else {
System.out.println("This spot is taken. Try entering a different
spot.");
return
false;
}
}
/**
* Checks if a player has made it three in a row
* @return true if a player won, false if no player has
won
*/
public boolean checkForWinner() {
for (int row = 0; row < 3;
row++) {
if(board[row][0]
== currentPlayer && board[row][1] == currentPlayer
&& board[row][2] == currentPlayer) {
winner = currentPlayer;
return true;
}
}
for(int col = 0; col < 3; col++)
{
if(board[0][col]
== currentPlayer && board[1][col] == currentPlayer
&& board[2][col] == currentPlayer) {
winner = currentPlayer;
return true;
}
}
if((board[0][0] == currentPlayer
&& board[1][1] == currentPlayer && board[2][2] ==
currentPlayer) || (board[0][2] == currentPlayer &&
board[1][1] == currentPlayer && board[2][0] ==
currentPlayer)) {
winner = currentPlayer;
return true;
}
return false;
}
/**
* Represents the board and players in string
form
* @return the board with X's and O's
*/
@Override
public String toString() {
String output = "| " + board[0][0]
+ " | " + board[1][0] + " | " + board[2][0] + " | \n";
output += "--------------
\n";
output += "| " + board[0][1] + " |
" + board[1][1] + " | " + board[2][1] + " | \n";
output += "--------------
\n";
output += "| " + board[0][2] + " |
" + board[1][2] + " | " + board[2][2] + " | \n";
return output;
}
/**
* Represents the main function and interacts with the
user(s) and calls the methods
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method
stub
int userRow;
int userCol;
int userAnswer;
boolean keepPlaying = true;
boolean gameWon = false;
TicTacToe object = new
TicTacToe();
Scanner keyboardIn = new
Scanner(System.in);
System.out.println("Welcome to Tic
Tac Toe");
//verify user input
do {
do {
System.out.println("Current Player:
" + object.getCurrentPlayer());
System.out.println(object.toString());
System.out.println("Enter a
column:");
userRow = keyboardIn.nextInt() -
1;
System.out.println("Enter a
row:");
userCol = keyboardIn.nextInt() -
1;
object.takeTurn(userRow,
userCol);
gameWon =
object.checkForWinner();
}while(gameWon
== false);
System.out.println("Do you want to play again? Enter 1 for yes or 2
for no.");
userAnswer =
keyboardIn.nextInt();
if(userAnswer ==
1) {
//call constructor to clear board
keepPlaying = true;
}
else {
System.out.println("Thanks for playing!");
keepPlaying = false;
}
}
while(keepPlaying == true);
keyboardIn.close();
}
}
What I currently have for my JUnit tests:
package cs145TicTacToe;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
/**
*
*/
class TicTacToeTest {
TicTacToe game;
/**
* @throws java.lang.Exception
*/
@BeforeEach
void setUp() throws Exception {
game = new TicTacToe();
game.getCurrentPlayer();
}
/**
* Test method for {@link
cs145TicTacToe.TicTacToe#TicTacToe()}.
*/
@Test
void testTicTacToe() {
assertEquals('X',
game.getCurrentPlayer());
assertEquals(' ',
game.getWinner());
//more with board?
}
/**
* Tests to make sure the program is returning the
correct current player
* Test method for {@link
cs145TicTacToe.TicTacToe#getCurrentPlayer()}.
*/
@Test
void testGetCurrentPlayer() {
assertEquals('X',
game.getCurrentPlayer());
}
/**
* Tests to make sure the program is returning the
correct winner
* Test method for {@link
cs145TicTacToe.TicTacToe#getWinner()}.
*/
@Test
void testGetWinner() {
fail("Not yet implemented");
}
/**
* Test method for {@link
cs145TicTacToe.TicTacToe#takeTurn(int, int)}.
*/
@Test
void testTakeTurn() {
fail("Not yet implemented");
}
/**
* Tests the checkForWinner() method
* Test method for {@link
cs145TicTacToe.TicTacToe#checkForWinner()}.
*/
@Test
void testCheckForWinner() {
fail("Not yet implemented");
}
/**
* Tests the toString() method and prints out the
board
* Test method for {@link
cs145TicTacToe.TicTacToe#toString()}.
*/
@Test
void testToString() {
assertEquals("| | | | \n" +
"-------------- \n" +
"| | | | \n" +
"-------------- \n" +
"| | | | \n", game.toString());
}
}
/**
*
*/
public class TicTacToeTest {
TicTacToe game;
/**
* @throws java.lang.Exception
*/
@BeforeEach
void setUp() throws Exception {
game = new
TicTacToe();
game.getCurrentPlayer();
}
/**
* Test method for {@link
cs145TicTacToe.TicTacToe#TicTacToe()}.
*/
@Test
void testTicTacToe() {
assertEquals('X',
game.getCurrentPlayer());
assertEquals(' ',
game.getWinner());
//more with board?
assertFalse(game.checkForWinner());
}
/**
* Tests to make sure the program is returning the
correct current player
* Test method for {@link
cs145TicTacToe.TicTacToe#getCurrentPlayer()}.
*/
@Test
void testGetCurrentPlayer() {
assertEquals('X',
game.getCurrentPlayer());
game.takeTurn(0,
0);
assertEquals('O',
game.getCurrentPlayer());
game.takeTurn(0,
0);
assertEquals('O',
game.getCurrentPlayer());
game.takeTurn(0,
1);
assertEquals('X',
game.getCurrentPlayer());
}
/**
* Tests to make sure the program is returning the
correct winner
* Test method for {@link
cs145TicTacToe.TicTacToe#getWinner()}.
*/
@Test
void testGetWinner() {
game.takeTurn(0,
0);
game.takeTurn(2,
0);
game.takeTurn(0,
1);
game.takeTurn(2,
1);
game.takeTurn(0,
2);
assertEquals('X',
game.getWinner());
}
/**
* Test method for {@link
cs145TicTacToe.TicTacToe#takeTurn(int, int)}.
*/
@Test
void testTakeTurn() {
game.takeTurn(0,
0);
assertEquals('O',
game.getCurrentPlayer());
game.takeTurn(0,
0);
assertEquals('O',
game.getCurrentPlayer());
game.takeTurn(0,
1);
assertEquals('X',
game.getCurrentPlayer());
}
/**
* Tests the checkForWinner() method
* Test method for {@link
cs145TicTacToe.TicTacToe#checkForWinner()}.
*/
@Test
void testCheckForWinner() {
game.takeTurn(0,
0);
game.takeTurn(2,
0);
game.takeTurn(1,
1);
game.takeTurn(2,
1);
game.takeTurn(2,
2);
assertTrue(game.checkForWinner());
}
/**
* Tests the toString() method and prints out the
board
* Test method for {@link
cs145TicTacToe.TicTacToe#toString()}.
*/
@Test
void testToString()
{
assertEquals("| | | |
\n" +
"--------------
\n" +
"| | | |
\n" +
"--------------
\n" +
"| | | |
\n", game.toString());
}
}
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.
This is my code for a TicTacToe game. However, I don't know how to write JUnit...
Please modify the following code to NOT have "TicTacToe extends Board" (I don't want any extending). Please ensure the resulting code completes EACH part of the following prompt: "Your task is to create a game of Tic-Tac-Toe using a 2-Dimensional String array as the game board. Start by creating a Board class that holds the array. The constructor should use a traditional for-loop to fill the array with "blank" Strings (eg. "-"). You may want to include other instance data......
This is my code for my game called Reversi, I need to you to make the Tester program that will run and complete the game. Below is my code, please add comments and Javadoc. Thank you. public class Cell { // Displays 'B' for the black disk player. public static final char BLACK = 'B'; // Displays 'W' for the white disk player. public static final char WHITE = 'W'; // Displays '*' for the possible moves available. public static...
Hello I am having trouble with a connectFour java program. this issue is in my findLocalWinner method, it declares a winner for horizontal wins, but not for vertical. if anyone can see what im doing wrong. public class ConnectFour { /** Number of columns on the board. */ public static final int COLUMNS = 7; /** Number of rows on the board. */ public static final int ROWS = 6; /** Character for computer player's pieces */ public static final...
JAVA TIC TAC TOE - please help me correct my code Write a program that will allow two players to play a game of TIC TAC TOE. When the program starts, it will display a tic tac toe board as below | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 The program will assign X to Player 1, and O to Player The program will ask Player 1, to...
JAVA Only Help on the sections that say Student provide code. The student Provide code has comments that i put to state what i need help with. import java.util.Scanner; public class TicTacToe { private final int BOARDSIZE = 3; // size of the board private enum Status { WIN, DRAW, CONTINUE }; // game states private char[][] board; // board representation private boolean firstPlayer; // whether it's player 1's move private boolean gameOver; // whether...
In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...
I just need a help in replacing player 2 and to make it unbeatable here is my code: #include<iostream> using namespace std; const int ROWS=3; const int COLS=3; void fillBoard(char [][3]); void showBoard(char [][3]); void getChoice(char [][3],bool); bool gameOver(char [][3]); int main() { char board[ROWS][COLS]; bool playerToggle=false; fillBoard(board); showBoard(board); while(!gameOver(board)) { getChoice(board,playerToggle); showBoard(board); playerToggle=!playerToggle; } return 1; } void fillBoard(char board[][3]) { for(int i=0;i<ROWS;i++) for(int j=0;j<COLS;j++) board[i][j]='*'; } void showBoard(char board[][3]) { cout<<" 1 2 3"<<endl; for(int i=0;i<ROWS;i++) { cout<<(i+1)<<"...
In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...
Please I need your help I have the code below but I need to add one thing, after player choose "No" to not play the game again, Add a HELP button or page that displays your name, the rules of the game and the month/day/year when you finished the implementation. import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import javax.swing.*; import javax.swing.event.*; import java.util.Random; public class TicTacToe extends JFrame implements ChangeListener, ActionListener { private JSlider slider; private JButton oButton, xButton; private Board...
Write Junit test for the following class below: public class Player { public int turnScore; public int roundScore; public int lastTurnScore; public String name; public int chipPile; public Player (String name) { this.name = name; this.turnScore = 0; this.chipPile = 50; } public int getChip() { return chipPile; } public void addChip(int chips) { chipPile...