Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Checking for Winners and Managing the Game All code is correct but there are two methods highlighted below where you need to do the logic

Checking for Winners and Managing the Game
All code is correct but there are two methods highlighted below where you need to do the logic for the game. Basically, checking the board to see if anyone has won and if there are more spaces to be selected.
import javax.swing.*;
import java.awt.geom.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
public class TicTacToe extends JPanel implements ActionListener
{
JLabel greeting = new JLabel("Tic Tac Toe");
JLabel promptLabel = new JLabel("Choose one button");
JLabel result = new JLabel();
Font headlineFont = new Font("Helvetica", Font.BOLD,
20);
Font mediumFont = new Font("Helvetica", Font.BOLD, 14);
final int SIZE =9;
final static int ROW_SIZE =3;
final static int COL_SIZE =3;
int x, y;
JButton[][] gameGrid = new JButton[ROW_SIZE][COL_SIZE];
final static String BLANK ="";
final static String XString ="X";
final static String OString ="O";
int row =0, col =0;
int num;
boolean spotFound;
boolean isDone = false;
boolean playersTurn = true;
boolean isWinPossible = false;
String msg ="";
String gridString;
char winChar;
public static boolean checkForWinner(JButton [][]
gameGrid)
{
boolean isDone = false;
// Your code goes here
// The code to check for winners needs to be implemented
// checkForWinner returns a boolean value representing a winner or not
return isDone;
}
public static boolean spacesRemain(JButton[][] grid)
{
// Your code goes here
// spacesRemain returns a boolean value indicating that there are stillDigitsLeft
// spaces to be selected.
return stillDigitsLeft;
}
public static void chooseSpot(JButton[][] gameGrid)
{
int x, y;
boolean placementMade = false;
for(x =0; x < ROW_SIZE && !placementMade; ++x)
{
if(gameGrid[x][0].getText().equals(OString) &&
gameGrid[x][1].getText().equals(OString) &&
gameGrid[x][2].getText().equals(BLANK))
{
gameGrid[x][2].setText(OString);
placementMade = true;
}
else
if(gameGrid[x][0].getText().equals(OString) &&
gameGrid[x][2].getText().equals(OString) &&
gameGrid[x][1].getText().equals(BLANK))
{
gameGrid[x][1].setText(OString);
placementMade = true;
}
else
if(gameGrid[x][1].getText().equals(OString) &&
gameGrid[x][2].getText().equals(OString) &&
gameGrid[x][0].getText().equals(BLANK))
{
gameGrid[x][0].setText(OString);
placementMade = true;
}
}
for(y =0; y < COL_SIZE && !placementMade; ++y)
{
if(gameGrid[0][y].getText().equals(OString) &&
gameGrid[1][y].getText().equals(OString) &&
gameGrid[2][y].getText().equals(BLANK))
{
gameGrid[2][y].setText(OString);
placementMade = true;
}
else
if(gameGrid[0][y].getText().equals(OString) &&
gameGrid[2][y].getText().equals(OString) &&
gameGrid[1][y].getText().equals(BLANK))
{
gameGrid[1][y].setText(OString);
placementMade = true;
}
else
if(gameGrid[1][y].getText().equals(OString) &&
gameGrid[2][y].getText().equals(OString) &&
gameGrid[0][y].getText

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

Learning MySQL Get A Handle On Your Data

Authors: Seyed M M Tahaghoghi

1st Edition

0596529465, 9780596529468

More Books

Students also viewed these Databases questions

Question

(12) What gaps are there in the current approach to development?

Answered: 1 week ago