Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In java: The goal of this assignment is develop a program to play a simple game

ID: 3705344 • Letter: I

Question

In java:

The goal of this assignment is develop a program to play a simple game which positions N Xs on an NxN board for any positive value of N such that no Xs are in the same row, the same column, or along the same diagonal. Your program should prompt the user for the number of Xs, find the solution (if it exists), and show the result in the format indicated below (shown here for N=4). Note that there are no blank lines between rows or spaces between columns.

*X**
***X
X***
**X*

If there is more than one solution, your program should only find and display one. Along with the solution, show the number of board positions evaluated in obtaining that solution. (A "board position" is defined as each unique arrangement of Xs. The illustration shown above is one board position. If the X on the bottom row was in the 2nd rather than the 3rd position, that would be a different board position.) If there is no solution, say so. At the user's discretion, show the changing board as the routine executes (showing each trial placement of Xs). Allow the user to play the game as many times as desired without exiting the program. To be effective, your program should have a systematic way to move Xs around on the board until a solution is found (or is determined to be impossible). It will also need a generic routine to check for conflicts (e.g., are there Xs on the same diagonal? etc.).

Requirement: This must be a recursive implementation. Suggestions: Try one new X position at a time. Pass the changing board and the trial X position as parameters. When a conflict exists, back up and try a new column position in the current row, and if necessary, back up to the previous row and try a new column position, etc. (recursion handles this nicely!). Be sure to make a copy of the board to pass on each recursive call. It may be helpful to construct a separate board class.

Explanation / Answer

4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 import java.util.Scanner; /** * Tic-Tac-Toe: Two-player console, non-graphics, non-OO version. * All variables/methods are declared as static (belong to the class) * in the non-OO version. */ public class TTTConsoleNonOO2P { // Name-constants to represent the seeds and cell contents public static final int EMPTY = 0; public static final int CROSS = 1; public static final int NOUGHT = 2; // Name-constants to represent the various states of the game public static final int PLAYING = 0; public static final int DRAW = 1; public static final int CROSS_WON = 2; public static final int NOUGHT_WON = 3; // The game board and the game status public static final int ROWS = 3, COLS = 3; // number of rows and columns public static int[][] board = new int[ROWS][COLS]; // game board in 2D array // containing (EMPTY, CROSS, NOUGHT) public static int currentState; // the current state of the game // (PLAYING, DRAW, CROSS_WON, NOUGHT_WON) public static int currentPlayer; // the current player (CROSS or NOUGHT) public static int currntRow, currentCol; // current seed's row and column public static Scanner in = new Scanner(System.in); // the input Scanner /** The entry main method (the program starts here) */ public static void main(String[] args) { // Initialize the game-board and current status initGame(); // Play the game once do { playerMove(currentPlayer); // update currentRow and currentCol updateGame(currentPlayer, currntRow, currentCol); // update currentState printBoard(); // Print message if game-over if (currentState == CROSS_WON) { System.out.println("'X' won! Bye!"); } else if (currentState == NOUGHT_WON) { System.out.println("'O' won! Bye!"); } else if (currentState == DRAW) { System.out.println("It's a Draw! Bye!"); } // Switch player currentPlayer = (currentPlayer == CROSS) ? NOUGHT : CROSS; } while (currentState == PLAYING); // repeat if not game-over } /** Initialize the game-board contents and the current states */ public static void initGame() { for (int row = 0; row
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote