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
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] + " | "; output += "-------------- "; output += "| " + board[0][1] + " | " + board[1][1] + " | " + board[2][1] + " | "; output += "-------------- "; output += "| " + board[0][2] + " | " + board[1][2] + " | " + board[2][2] + " | "; 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("| | | | " + "-------------- " + "| | | | " + "-------------- " + "| | | | ", game.toString()); }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started