Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a test-first development using JUnit on a tic tac toe game from this code, the test should be about checking the winner. import java.util.Arrays;

Write a test-first development using JUnit on a tic tac toe game from this code, the test should be about checking the winner.

import java.util.Arrays; import java.util.InputMismatchException; import java.util.Scanner; public class TicTacToeGame { static Scanner in; static String[] board; static String turn; public static void main(String[] args) { in = new Scanner(System.in); board = new String[9]; turn = "S"; String winner = null; populateEmptyBoard(); System.out.println("Welcome to 2 Player Tic Tac Toe Game."); System.out.println("--------------------------------"); printBoard(); System.out.println("S's will play first. Enter a slot number to place S in:"); while (winner == null) { int numInput; try { numInput = in.nextInt(); if (!(numInput > 0 && numInput <= 9)) { System.out.println("Invalid input:plese re-enter slot number:"); continue; } } catch (InputMismatchException e) { System.out.println("Invalid input; please re-enter slot number:"); continue; } if (board[numInput-1].equals(String.valueOf(numInput))) { board[numInput-1] = turn; if (turn.equals("S")) { turn = "O"; } else { turn = "S"; } printBoard(); winner = checkWinner(); } else { System.out.println("Slot already taken:please re-enter slot number:"); continue; } } if (winner.equalsIgnoreCase("draw/tie")) { System.out.println("It's a draw! Thanks for playing the tic tac toe game."); } else { System.out.println("Congratulations! " + winner + "'s have won! Thanks for playing the tic tac toe game."); } } static String checkWinner() { for (int a = 0; a < 8; a++) { String line = null; switch (a) { case 0: line = board[0] + board[1] + board[2]; break; case 1: line = board[3] + board[4] + board[5]; break; case 2: line = board[6] + board[7] + board[8]; break; case 3: line = board[0] + board[3] + board[6]; break; case 4: line = board[1] + board[4] + board[7]; break; case 5: line = board[2] + board[5] + board[8]; break; case 6: line = board[0] + board[4] + board[8]; break; case 7: line = board[2] + board[4] + board[6]; break; } if (line.equals("SSS")) { return "S"; } else if (line.equals("OOO")) { return "O"; } } for (int a = 0; a < 9; a++) { if (Arrays.asList(board).contains(String.valueOf(a+1))) { break; } else if (a == 8) return "draw/tie"; } System.out.println(turn + "'s turn; enter a slot number to place " + turn + " in:"); return null; } static void printBoard() { System.out.println("/---|---|---\\"); System.out.println("| " + board[0] + " | " + board[1] + " | " + board[2] + " |"); System.out.println("|-----------|"); System.out.println("| " + board[3] + " | " + board[4] + " | " + board[5] + " |"); System.out.println("|-----------|"); System.out.println("| " + board[6] + " | " + board[7] + " | " + board[8] + " |"); System.out.println("/---|---|---\\"); } static void populateEmptyBoard() { for (int a = 0; a < 9; a++) { board[a] = String.valueOf(a+1); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 14th East European Conference Adbis 2010 Novi Sad Serbia September 2010 Proceedings Lncs 6295

Authors: Barbara Catania ,Mirjana Ivanovic ,Bernhard Thalheim

2010th Edition

3642155758, 978-3642155758

More Books

Students also viewed these Databases questions

Question

4. How is culture a contested site?

Answered: 1 week ago