Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with this code. I added endgame method to restart the game is user entered 'y'. It's not working the way It's suppost

I need help with this code. I added endgame method to restart the game is user entered 'y'. It's not working the way It's suppost to and some help would be greatly appriciated. Right now the code doesn't let me finish the game whether its a tie or a win, it doesn't let you place the final X or O to win.

import java.awt.Color; import javax.swing.JFrame; import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.Scanner; import javax.swing.JPanel; class Ttc { public static void main(String[] args) { JFrame frame = new JFrame("Tic Tac Toe 2.0"); frame.getContentPane().add(new ttcPanel()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); frame.setVisible(true); frame.setBackground(Color.BLACK); frame.pack(); } } class ttcPanel extends JPanel{ private int boxX, boxY, clickCounter; private boolean box1, box2, box3, box4, box5, box6, box7, box8, box9; private Color cbox1, cbox2, cbox3, cbox4, cbox5, cbox6, cbox7, cbox8, cbox9; private boolean endgame; char player = 'X'; public ttcPanel() { addMouseListener(new coordinateListener()); clickCounter = 0; setPreferredSize(new Dimension(320, 410)); } public void restart(){ boxX = 0; boxY = 0; clickCounter = 0; box1 = false; box2 = false; box3 = false; box4 = false; box5 = false; box6 = false; box7 = false; box8 = false; box9 = false; cbox1 = null; cbox2 = null; cbox3 = null; cbox4 = null; cbox5 = null; cbox6 = null; cbox7 = null; cbox8 = null; cbox9 = null; endgame = false; player = 'X'; } public class coordinateListener implements MouseListener { @Override public void mouseClicked(MouseEvent e) { boxX = e.getX(); boxY = e.getY(); clickCounter++; } @Override public void mousePressed(MouseEvent e) {} @Override public void mouseReleased(MouseEvent e) {} @Override public void mouseEntered(MouseEvent e) {} @Override public void mouseExited(MouseEvent e) {} } @Override public void paintComponent(Graphics g) { Color color; g.setColor(Color.WHITE); g.setFont(new Font("monospaced", Font.PLAIN, 145)); if(clickCounter %2 != 0) { player = 'O'; color = (Color.BLUE); g.setColor(Color.BLUE); } else { player = 'X'; color =(Color.RED); g.setColor(Color.RED); } if(boxX >=10 && boxX <= 110 && boxY >=100 && boxY <=200 && !box1) { g.drawString(player + "", 10, 190); box1 = true; cbox1 = color; } if(boxX >=110 && boxX <= 210 && boxY >=100 && boxY <=200 && !box2) { g.drawString(player + "", 110, 190); box2 = true; cbox2 = color; } if(boxX >=210 && boxX <= 310 && boxY >=100 && boxY <=200 && !box3) { g.drawString(player + "", 210, 190); box3 = true; cbox3 = color; } if(boxX >=10 && boxX <= 110 && boxY >=200 && boxY <=300 && !box4) { g.drawString(player + "", 10, 300); box4 = true; cbox4 = color; } if(boxX >=110 && boxX <= 210 && boxY >=200 && boxY <=300 && !box5) { g.drawString(player + "", 110, 300); box5 = true; cbox5 = color; } if(boxX >=210 && boxX <= 310 && boxY >=200 && boxY <=300 && !box6) { g.drawString(player + "", 210, 300); box6 = true; cbox6 = color; } if(boxX >=10 && boxX <= 110 && boxY >=300 && boxY <=400 && !box7) { g.drawString(player + "", 10, 400); box7 = true; cbox7 = color; } if(boxX >=110 && boxX <= 210 && boxY >=300 && boxY <=400 && !box8) { g.drawString(player + "", 110, 400); box8 = true; cbox8 = color; } if(boxX >=210 && boxX <= 310 && boxY >=300 && boxY <=400 && !box9) { g.drawString(player + "", 210, 400); box9 = true; cbox9 = color; } g.setColor(Color.WHITE); for(int x = 10; x<=210; x+=100) { for(int y = 100; y<=300; y+=100) { g.drawRect(x, y, 100, 100); } } g.setFont(new Font("Helvetica", Font.PLAIN, 24)); g.setColor(Color.GREEN); if(cbox1==Color.red && cbox2==Color.red && cbox3==Color.red) { g.drawString("The winner is: Red", 10, 50); endgame = true; } else if(cbox4==Color.red && cbox5==Color.red && cbox6==Color.red) { g.drawString("The winner is: Red", 10, 50); endgame = true; } else if(cbox7==Color.red && cbox8==Color.red && cbox9==Color.red) { g.drawString("The winner is: Red", 10, 50); endgame = true; } else if(cbox1==Color.red && cbox4==Color.red && cbox7==Color.red) { g.drawString("The winner is: Red", 10, 50); endgame = true; } else if(cbox2==Color.red && cbox5==Color.red && cbox8==Color.red) { g.drawString("The winner is: Red", 10, 50); endgame = true; } else if(cbox3==Color.red && cbox6==Color.red && cbox9==Color.red) { g.drawString("The winner is: Red", 10, 50); endgame = true; } else if(cbox1==Color.red && cbox5==Color.red && cbox9==Color.red) { g.drawString("The winner is: Red", 10, 50); endgame = true; } else if(cbox3==Color.red && cbox5==Color.red && cbox7==Color.red) { g.drawString("The winner is: Red", 10, 50); endgame = true; } else if(cbox1==Color.blue && cbox2==Color.blue && cbox3==Color.blue) { g.drawString("The winner is: blue", 10, 50); endgame = true; } else if(cbox4==Color.blue && cbox5==Color.blue && cbox6==Color.blue) { g.drawString("The winner is: blue", 10, 50); endgame = true; } else if(cbox7==Color.blue && cbox8==Color.blue && cbox9==Color.blue) { g.drawString("The winner is: blue", 10, 50); endgame = true; } else if(cbox1==Color.blue && cbox4==Color.blue && cbox7==Color.blue) { g.drawString("The winner is: blue", 10, 50); endgame = true; } else if(cbox2==Color.blue && cbox5==Color.blue && cbox8==Color.blue) { g.drawString("The winner is: blue", 10, 50); endgame = true; } else if(cbox3==Color.blue && cbox6==Color.blue && cbox9==Color.blue) { g.drawString("The winner is: blue", 10, 50); endgame = true; } else if(cbox1==Color.blue && cbox5==Color.blue && cbox9==Color.blue) { g.drawString("The winner is: blue", 10, 50); endgame = true; } else if(cbox3==Color.blue && cbox5==Color.blue && cbox7==Color.blue) { g.drawString("The winner is: blue", 10, 50); endgame = true; } if (endgame) { Scanner s = new Scanner(System.in); g.drawString("Would you like to play again y/n?", 10, 100); String n = s.nextLine(); if (n.equals("y")); { restart(); } if (n.equals("n")); { repaint(); } } repaint(); } }

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

Database Concepts

Authors: David Kroenke, David J. Auer

3rd Edition

0131986252, 978-0131986251

More Books

Students also viewed these Databases questions

Question

2. Are my sources up to date?

Answered: 1 week ago