How do I code for the following JUnit tests? // ADDITIONAL TEST CASES TO IMPLEMENT: // Play game to completion, where there is a winner // Input where the q comes instead of an integer for the row // Input where the q comes instead of an integer for the column // Input where non-integer garbage comes instead of an integer for the row // Input where non-integer garbage comes instead of an integer for the column // Input where the move is integers, but outside the bounds of the board // Input where the move is integers, but invalid because the cell is occupied // Multiple invalid moves in a row of various kinds // Input including valid moves interspersed with invalid moves, game is played to completion // What happens when the input ends "abruptly" -- no more input, but not quit, and game not over public class TicTacToeControllerTest { @Test public void testSingleValidMove() { TicTacToe m = new TicTacToeModel(); StringBuilder gameLog = new StringBuilder(); TicTacToeController c = new TicTacToeConsoleController(new StringReader("2 2 q"), gameLog); c.playGame(m); assertEquals(" | | \n" + "-----------\n" + " | | \n" + "-----------\n" + " | | \n" + "Enter a move for X:\n" + " | | \n" + "-----------\n" + " | X | \n" + "-----------\n" + " | | \n" + "Enter a move for O:\n" + "Game quit! Ending game state:\n" + " | | \n" + "-----------\n" + " | X | \n" + "-----------\n" + " | | \n", gameLog.toString()); } @Test public void testBogusInputAsRow() { TicTacToe m = new TicTacToeModel(); StringReader input = new StringReader("!#$ 2 q"); StringBuilder gameLog = new StringBuilder(); TicTacToeController c = new TicTacToeConsoleController(input, gameLog); c.playGame(m); // split the output into an array of lines String[] lines = gameLog.toString().split("\n"); // check that it's the correct number of lines assertEquals(13, lines.length); // check that the last 6 lines are correct String lastMsg = String.join("\n", Arrays.copyOfRange(lines, lines.length - 6, lines.length)); assertEquals("Game quit! Ending game state:\n" + " | | \n" + "-----------\n" + " | | \n" + "-----------\n" + " | | ", lastMsg); // note no trailing \n here, because of the earlier split } @Test public void testTieGame() { TicTacToe m = new TicTacToeModel(); // note the entire sequence of user inputs for the entire game is in this one string: StringReader input = new StringReader("2 2 1 1 3 3 1 2 1 3 2 3 2 1 3 1 3 2"); StringBuilder gameLog = new StringBuilder(); TicTacToeController c = new TicTacToeConsoleController(input, gameLog); c.playGame(m); String[] lines = gameLog.toString().split("\n"); assertEquals(60, lines.length); assertEquals("Game is over! Tie game.", lines[lines.length - 1]); }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

How do I code for the following JUnit tests?

// ADDITIONAL TEST CASES TO IMPLEMENT:
// Play game to completion, where there is a winner
// Input where the q comes instead of an integer for the row
// Input where the q comes instead of an integer for the column
// Input where non-integer garbage comes instead of an integer for the row
// Input where non-integer garbage comes instead of an integer for the column
// Input where the move is integers, but outside the bounds of the board
// Input where the move is integers, but invalid because the cell is occupied
// Multiple invalid moves in a row of various kinds
// Input including valid moves interspersed with invalid moves, game is played to completion
// What happens when the input ends "abruptly" -- no more input, but not quit, and game not over

public class TicTacToeControllerTest {

@Test
public void testSingleValidMove() {
TicTacToe m = new TicTacToeModel();
StringBuilder gameLog = new StringBuilder();
TicTacToeController c = new TicTacToeConsoleController(new StringReader("2 2 q"), gameLog);
c.playGame(m);
assertEquals(" | | \n"
+ "-----------\n"
+ " | | \n"
+ "-----------\n"
+ " | | \n"
+ "Enter a move for X:\n"
+ " | | \n"
+ "-----------\n"
+ " | X | \n"
+ "-----------\n"
+ " | | \n"
+ "Enter a move for O:\n"
+ "Game quit! Ending game state:\n"
+ " | | \n"
+ "-----------\n"
+ " | X | \n"
+ "-----------\n"
+ " | | \n", gameLog.toString());
}

@Test
public void testBogusInputAsRow() {
TicTacToe m = new TicTacToeModel();
StringReader input = new StringReader("!#$ 2 q");
StringBuilder gameLog = new StringBuilder();
TicTacToeController c = new TicTacToeConsoleController(input, gameLog);
c.playGame(m);
// split the output into an array of lines
String[] lines = gameLog.toString().split("\n");
// check that it's the correct number of lines
assertEquals(13, lines.length);
// check that the last 6 lines are correct
String lastMsg = String.join("\n",
Arrays.copyOfRange(lines, lines.length - 6, lines.length));
assertEquals("Game quit! Ending game state:\n"
+ " | | \n"
+ "-----------\n"
+ " | | \n"
+ "-----------\n"
+ " | | ", lastMsg);
// note no trailing \n here, because of the earlier split
}

@Test
public void testTieGame() {
TicTacToe m = new TicTacToeModel();
// note the entire sequence of user inputs for the entire game is in this one string:
StringReader input = new StringReader("2 2 1 1 3 3 1 2 1 3 2 3 2 1 3 1 3 2");
StringBuilder gameLog = new StringBuilder();
TicTacToeController c = new TicTacToeConsoleController(input, gameLog);
c.playGame(m);
String[] lines = gameLog.toString().split("\n");
assertEquals(60, lines.length);
assertEquals("Game is over! Tie game.", lines[lines.length - 1]);
}

@Test(expected = IllegalStateException.class)
public void testFailingAppendable() {
TicTacToe m = new TicTacToeModel();
StringReader input = new StringReader("2 2 1 1 3 3 1 2 1 3 2 3 2 1 3 1 3 2");
Appendable gameLog = new FailingAppendable();
TicTacToeController c = new TicTacToeConsoleController(input, gameLog);
c.playGame(m);
}

}

In the starter code (at the bottom of this page), you are given an interface representing a controller for Tic Tac Toe, with a single method, playGame(). Your task is to implement the TicTacToeController interface. Put your new controller code in the same package alongside your Tic Tac Toe model from the previous exercise as it will depend on the model. You are also given a class Main with a main() method that will allow you to test your game interactively.

A single move consists of two numbers, specifying the row and column of the intended move position. Board positions for these moves are numbered from 1. For example, to mark X in the upper left cell, the user would enter "1 1" at the first prompt. To mark O in the upper right cell on the second move, the user would enter "1 3". To quit a game in progress, the user can enter q or Q at any time.

The game state is the output of the model’s toString() method, followed by a carriage return (\n). The move prompt is

"Enter a move for " + model.getTurn().toString() + ":\n"

(where model is an instance of your Tic Tac Toe Model).

If a non-integer value is entered, it should be rejected with an error message. If an invalid move is entered, namely, two valid integers, but the proposed move was deemed invalid by the model, the controller should give an error message. The message text is up to you, but should end with a carriage return.

At the end of the game, the controller should output, in order on separate lines:

  • A final game state

  • "Game is over!" followed by "X wins." or "O wins." or "Tie game." depending on the outcome

If the user quits, the controller should output

"Game quit! Ending game state:\n" + model.toString() + "\n" and end the playGame() method.

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Adjacency Matrix
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education