Categories: Quick Homework

Checking for Winners and Manage Game Project Checking for Winners and Managing the Game All code is correct but there are two methods below in red where y

Checking for Winners and Manage Game Project Checking for Winners and Managing the Game

All code is correct but there are two methods below in red 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.

Don't use plagiarized sources. Get Your Custom Essay on
Checking for Winners and Manage Game Project Checking for Winners and Managing the Game All code is correct but there are two methods below in red where y
Get an essay WRITTEN FOR YOU, Plagiarism free, and by an EXPERT!
Order Essay

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().equals(BLANK)) { gameGrid[0][y].setText(OString); placementMade = true; } } if(!placementMade) if(gameGrid[0][0].getText().equals(OString) && gameGrid[1][1].getText().equals(OString) && gameGrid[2][2].getText().equals(BLANK)) { gameGrid[2][2].setText(OString); placementMade = true; } else if(gameGrid[0][0].getText().equals(OString) && gameGrid[2][2].getText().equals(OString) && gameGrid[1][1].getText().equals(BLANK)) { gameGrid[1][1].setText(OString); placementMade = true; } else if(gameGrid[2][2].getText().equals(OString) && gameGrid[1][1].getText().equals(OString) && gameGrid[0][0].getText().equals(BLANK)) { gameGrid[0][0].setText(OString); placementMade = true; } else if(gameGrid[0][2].getText().equals(OString) && gameGrid[1][1].getText().equals(OString) && gameGrid[2][0].getText().equals(BLANK)) { gameGrid[2][0].setText(OString); placementMade = true; } else if(gameGrid[0][2].getText().equals(OString) && gameGrid[2][0].getText().equals(OString) && gameGrid[1][1].getText().equals(BLANK)) { gameGrid[1][1].setText(OString); placementMade = true; } else if(gameGrid[1][1].getText().equals(OString) && gameGrid[2][0].getText().equals(OString) && gameGrid[0][2].getText().equals(BLANK)) { gameGrid[0][2].setText(OString); placementMade = true; } if(!placementMade) { while(!placementMade) { placementMade = true; int num = (int)((Math.random() * 100) % 9); x = num / 3; y = num % 3; if(!gameGrid[x][y].getText().equals(BLANK)) placementMade = false; } gameGrid[x][y].setText(OString); } } public TicTacToe() { greeting.setFont(headlineFont); add(greeting); promptLabel.setFont(mediumFont); add(promptLabel); for(x = 0; x < ROW_SIZE; ++x) for(y = 0; y < COL_SIZE; ++y) { gameGrid[x][y] = new JButton(); gameGrid[x][y].setText(BLANK); add(gameGrid[x][y]); gameGrid[x][y].addActionListener(this); } add(result); } @Override public void actionPerformed(ActionEvent e) { isDone = false; Object squareChosen = e.getSource(); for(x = 0; x < ROW_SIZE; ++x) for(y = 0; y < COL_SIZE; ++y) if(squareChosen == gameGrid[x][y]) { row = x; col = y; x = ROW_SIZE; y = COL_SIZE; } if(gameGrid[row][col].getText().equals(XString) || gameGrid[row][col].getText().equals(OString)) { msg = "That position is already takenn"; } else { gameGrid[row][col].setText(XString); msg = ""; isDone = checkForWinner(gameGrid); msg = ""; if(isDone) { msg = "You win! "; winChar = 'x'; } else if(!spacesRemain(gameGrid)) { isDone = true; msg = " Tie game"; winChar = 't'; } if(isDone) result.setText("Game over! " + msg); else { chooseSpot(gameGrid); isDone = checkForWinner(gameGrid); if(isDone) { result.setText("Sorry - you lose"); winChar = 'o'; } } } if(isDone) repaint(); } @Override public void paintComponent(Graphics gr) { super.paintComponent(gr); float startx = 30f; float starty = 210f; float width = 50f; float height = 50f; Graphics2D g = (Graphics2D)gr; BasicStroke aStroke = new BasicStroke(6.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND); g.setStroke(aStroke); g.setColor(Color.BLUE); if(winChar == 'x') { g.draw(new Line2D.Float(startx, starty, startx + width, starty + height)); g.draw(new Line2D.Float(startx, starty + height, startx + width, starty)); } else if(winChar == 'o') { g.draw(new Ellipse2D.Float(startx, starty, width, height)); } else if(winChar == 't') { g.draw(new Line2D.Float(startx, starty, startx + width, starty + height)); g.draw(new Line2D.Float(startx, starty + height, startx + width, starty)); g.setColor(Color.RED); g.draw(new Ellipse2D.Float(startx, starty, width, height)); } } public static void main(String[] args) { JFrame frame = new JFrame(); frame.add(new TicTacToe()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 300); frame.setVisible(true); } } Submission Details: Embed your programs in a Microsoft Word document with a description of your programming strategy. Name your document SU_ITS3105_W5_Project_LastName_FirstInitial.doc.

superadmin

Recent Posts

What is the easy difination of science | Quick Solution

Science is the pursuit and application of knowledge and understanding of the natural and social…

3 years ago

definition, values, meaning of such values and type of goods with such elasticity value …….. | Quick Solution

Clearly stating the definition, the values, the meaning of such values and the type of…

3 years ago

Acct 422 – Nora D | Quick Solution

All answered must be typed using Times New Roman (size 12, double-spaced) font. No pictures…

3 years ago

Acct 322 – Nora D | Quick Solution

All answered must be typed using Times New Roman (size 12, double-spaced) font. No pictures…

3 years ago

Macro Economics Question | Quick Solution

https://www.npr.org/sections/ed/2018/04/25/605092520/high-paying-trade-jobs-sit-empty-while-high-school-grads-line-up-for-university Click on the link above. Read the entire link and answer the questions below…

3 years ago

MGT 322 – Nora D | Quick Solution

All answered must be typed using Times New Roman (size 12, double-spaced) font. No pictures…

3 years ago