Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA: Tic Tac Toe Question I am to create a method checkWincondition to check to see if the game wins horizontally, vertically, and diagonallly. However,

JAVA: Tic Tac Toe Question

I am to create a method checkWincondition to check to see if the game wins horizontally, vertically, and diagonallly.

However, when I try to run my code it give me an error saying, I believe that my method is working, but maybe another method is triggering its error?image text in transcribed

The Method to check:image text in transcribed

I dont know if any of my other methods are affecting it or not, so I should post my whole code here as well.

my code whole code is:

public class Application { /*Global Variables*/ static String[] visualGameBoard = new String[7]; static int[][] gameBoard = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}; public static void main(String[] args) { updateGameBoard(); initBoard(); applyUserInput(1, true); applyUserInput(2, true); applyUserInput(3, true); checkWinCondition(); printGameBoard(); } public static void initBoard() { /*Creating Printable Board for Tic Tac Toe Board*/ visualGameBoard[0] = (" --- --- ---"); visualGameBoard[1] = (" | | | |"); visualGameBoard[2] = (" --- --- ---"); visualGameBoard[3] = (" | | | |"); visualGameBoard[4] = (" --- --- ---"); visualGameBoard[5] = (" | | | |"); visualGameBoard[6] = (" --- --- ---"); } public static void printGameBoard() { /*Prints the GameBoard */ for (String s : visualGameBoard) { System.out.println(s); } } public static boolean applyUserInput(int userInput, boolean player) { /*Input parameters*/ if (userInput  9) { System.out.println("Please enter a valid number between 1 and 9, try again..."); return false; /*Nested If-Statements to print out failed inputs and successful inputs*/ } else if (userInput == 1) { if (gameBoard[0][0] != 0) { System.out.println("Sorry that spot is already taken please try again!"); return false; } else { if (player) { gameBoard[0][0] = 1; } else { gameBoard[0][0] = 2; } } } else if (userInput == 2) { if (gameBoard[0][1] != 0) { System.out.println("Sorry that spot is already taken please try again!"); return false; } else { if (player) { gameBoard[0][1] = 1; } else { gameBoard[0][1] = 2; } } } else if (userInput == 3) { if (gameBoard[0][2] != 0) { System.out.println("Sorry that spot is already taken please try again!"); return false; } else { if (player) { gameBoard[0][2] = 1; } else { gameBoard[0][2] = 2; } } } else if (userInput == 4) { if (gameBoard[1][0] != 0) { System.out.println("Sorry that spot is already taken please try again!"); return false; } else { if (player) { gameBoard[1][0] = 1; } else { gameBoard[1][0] = 2; } } } else if (userInput == 5) { if (gameBoard[1][1] != 0) { System.out.println("Sorry that spot is already taken please try again!"); return false; } else { if (player) { gameBoard[1][1] = 1; } else { gameBoard[1][1] = 2; } } } else if (userInput == 6) { if (gameBoard[1][2] != 0) { System.out.println("Sorry that spot is already taken please try again!"); return false; } else { if (player) { gameBoard[1][2] = 1; } else { gameBoard[1][2] = 2; } } } else if (userInput == 7) { if (gameBoard[2][0] != 0) { System.out.println("Sorry that spot is already taken please try again!"); return false; } else { if (player) { gameBoard[2][0] = 1; } else { gameBoard[2][0] = 2; } } } else if (userInput == 8) { if (gameBoard[2][1] != 0) { System.out.println("Sorry that spot is already taken please try again!"); return false; } else { if (player) { gameBoard[2][1] = 1; } else { gameBoard[2][1] = 2; } } } else if (userInput == 9) { if (gameBoard[2][2] != 0) { System.out.println("Sorry that spot is already taken please try again!"); return false; } else { if (player) { gameBoard[2][2] = 1; } else { gameBoard[2][2] = 2; } } } updateGameBoard(); return true; } public static void updateGameBoard() { /* Creating a Nested-Loop to check 2D array, Then creating Substrings to input X or O's to wherever 1 or 2 are */ for (int row = 0; row   5: Unit Test 5 A 0/8 Test check WinCondition method Your output Sorry that spot is already taken please try again! Test feedback Fail: For Veritcal win check: Your applyUserInput returned incorrectly g Expected: true Received: false The consecutive inputs to your applyUser Input method were: 1, true; public static boolean checkWinCondition() { /*Horizontal Connections */ if (gameBoard[@][0] == 1 && gameBoard[0][1] == 1 && gameBoard[@][2] == 1) { return true; } else if (gameBoard[1][0] == 1 && gameBoard[1][1] == 1 && gameBoard[1][2] == 1) { return true; } else if (gameBoard[2][@] == 1 && gameBoard[2][1] == 1 && gameBoard[2][2] == 1) { return true;| /*Vertical Connection*/ } else if (gameBoard[0][0] == 1 && gameBoard[1][@] == 1 && gameBoard[2][@] == 1) { return true; } else if (gameBoard[0][1] == 1 && gameBoard[1][1] == 1 && gameBoard[2][1] == 1) { return true; } else if (gameBoard[0][2] == 1 && gameBoard[1][2] == 1 && gameBoard[2][2] == 1) { return true; /*Diagonal Connection* / else if (gameBoard[@][0] == 1 && gameBoard[1][1] == 1 && gameBoard[2][2] == 1) { return true; } else if (gameBoard[@][2] == 1 && gameBoard[1][1] == 1 && gameBoard[2][@] == 1) { return true; } else { return false

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions