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.
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.
Why Work with Us
Top Quality and Well-Researched Papers
We always make sure that writers follow all your instructions precisely. You can choose your academic level: high school, college/university or professional, and we will assign a writer who has a respective degree.
Professional and Experienced Academic Writers
We have a team of professional writers with experience in academic and business writing. Many are native speakers and able to perform any task for which you need help.
Free Unlimited Revisions
If you think we missed something, send your order for a free revision. You have 10 days to submit the order for review after you have received the final document. You can do this yourself after logging into your personal account or by contacting our support.
Prompt Delivery and 100% Money-Back-Guarantee
All papers are always delivered on time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.
Original & Confidential
We use several writing tools checks to ensure that all documents you receive are free from plagiarism. Our editors carefully review all quotations in the text. We also promise maximum confidentiality in all of our services.
24/7 Customer Support
Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.
Try it now!
How it works?
Follow these simple steps to get your paper done
Place your order
Fill in the order form and provide all details of your assignment.
Proceed with the payment
Choose the payment system that suits you most.
Receive the final file
Once your paper is ready, we will email it to you.
Our Services
No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.
Essays
No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.
Admissions
Admission Essays & Business Writing Help
An admission essay is an essay or other written statement by a candidate, often a potential student enrolling in a college, university, or graduate school. You can be rest assurred that through our service we will write the best admission essay for you.
Reviews
Editing Support
Our academic writers and editors make the necessary changes to your paper so that it is polished. We also format your document by correctly quoting the sources and creating reference lists in the formats APA, Harvard, MLA, Chicago / Turabian.
Reviews
Revision Support
If you think your paper could be improved, you can request a review. In this case, your paper will be checked by the writer or assigned to an editor. You can use this option as many times as you see fit. This is free because we want you to be completely satisfied with the service offered.