Я пытаюсь заставить мое TicTacToe работать, но моя победная или потерянная шашка не работает.TicTacToe win check не работает
это мой старт-рамка.
package tictactoe;
import javax.swing.*;
public class TTT extends JFrame {
public static void main(String args[]) {
JFrame frame = new TTT();
frame.setSize(695, 620);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("TicTacToe");
JPanel paneel = new paneel();
frame.setContentPane(paneel);
frame.setVisible(true);
frame.setLocation(400, 300);
}
}
и это мои кнопки и панель. упаковка tictactoe;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;
class paneel extends JPanel {
private JButton start, exit, TTT1, TTT2, TTT3, TTT4, TTT5, TTT6, TTT7, TTT8, TTT9;
private JTextField roundInt;
static String x = "X";
static String o = "O";
static int round = 1;
static boolean playerTurn = true;
public paneel() {
setLayout(null);
setBackground(Color.DARK_GRAY);
start = new JButton("Start");
start.addActionListener(new startHandler());
start.setBounds(100,100,400,100);
start.setBackground(Color.GRAY);
start.setForeground(Color.black);
start.setVisible(true);
start.setOpaque(true);
exit = new JButton("exit");
exit.addActionListener(new exitHandler());
exit.setBounds(100,300,400,100);
exit.setBackground(Color.GRAY);
exit.setForeground(Color.BLACK);
exit.setVisible(true);
exit.setOpaque(true);
roundInt = new JTextField(round);
roundInt.setBackground(Color.DARK_GRAY);
roundInt.setForeground(Color.WHITE);
roundInt.setBounds(590,20,70,40);
roundInt.setVisible(false);
TTT1 = new JButton();
TTT2 = new JButton();
TTT3 = new JButton();
TTT4 = new JButton();
TTT5 = new JButton();
TTT6 = new JButton();
TTT7 = new JButton();
TTT8 = new JButton();
TTT9 = new JButton();
TTT1.addActionListener(new ttt1());
TTT2.addActionListener(new ttt2());
TTT3.addActionListener(new ttt3());
TTT4.addActionListener(new ttt4());
TTT5.addActionListener(new ttt5());
TTT6.addActionListener(new ttt6());
TTT7.addActionListener(new ttt7());
TTT8.addActionListener(new ttt8());
TTT9.addActionListener(new ttt9());
TTT1.setBounds(20,20,170,170);
TTT2.setBounds(200,20,170,170);
TTT3.setBounds(380,20,170,170);
TTT4.setBounds(20,200,170,170);
TTT5.setBounds(200,200,170,170);
TTT6.setBounds(380,200,170,170);
TTT7.setBounds(20,380,170,170);
TTT8.setBounds(200,380,170,170);
TTT9.setBounds(380,380,170,170);
TTT1.setBackground(Color.decode("0x999999"));
TTT2.setBackground(Color.decode("0x999999"));
TTT3.setBackground(Color.decode("0x999999"));
TTT4.setBackground(Color.decode("0x999999"));
TTT5.setBackground(Color.decode("0x999999"));
TTT6.setBackground(Color.decode("0x999999"));
TTT7.setBackground(Color.decode("0x999999"));
TTT8.setBackground(Color.decode("0x999999"));
TTT9.setBackground(Color.decode("0x999999"));
TTT1.setVisible(false);
TTT2.setVisible(false);
TTT3.setVisible(false);
TTT4.setVisible(false);
TTT5.setVisible(false);
TTT6.setVisible(false);
TTT7.setVisible(false);
TTT8.setVisible(false);
TTT9.setVisible(false);
TTT1.setForeground(Color.decode("0x000000"));
TTT2.setForeground(Color.decode("0x000000"));
TTT3.setForeground(Color.decode("0x000000"));
TTT4.setForeground(Color.decode("0x000000"));
TTT5.setForeground(Color.decode("0x000000"));
TTT6.setForeground(Color.decode("0x000000"));
TTT7.setForeground(Color.decode("0x000000"));
TTT8.setForeground(Color.decode("0x000000"));
TTT9.setForeground(Color.decode("0x000000"));
add(start);
add(exit);
add(TTT1);
add(TTT2);
add(TTT3);
add(TTT4);
add(TTT5);
add(TTT6);
add(TTT7);
add(TTT8);
add(TTT9);
add(roundInt);
}
class exitHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
class startHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
start.setVisible(false);
exit.setVisible(false);
roundInt.setVisible(true);
TTT1.setVisible(true);
TTT2.setVisible(true);
TTT3.setVisible(true);
TTT4.setVisible(true);
TTT5.setVisible(true);
TTT6.setVisible(true);
TTT7.setVisible(true);
TTT8.setVisible(true);
TTT9.setVisible(true);
}
}
class ttt1 implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(playerTurn){
TTT1.setText("X");
playerTurn = false;
wincheck.knop1C = 1;
round++;
}else{
TTT1.setText("O");
playerTurn = true;
wincheck.knop1C = 2;
round++;
}
}
}
class ttt2 implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(playerTurn){
TTT2.setText("X");
playerTurn = false;
wincheck.knop2C = 1;
round++;
}else{
TTT2.setText("O");
playerTurn = true;
wincheck.knop2C = 2;
round++;
}
}
}
class ttt3 implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(playerTurn){
TTT3.setText("X");
playerTurn = false;
wincheck.knop3C = 1;
round++;
}else{
TTT3.setText("O");
playerTurn = true;
wincheck.knop3C = 2;
round++;
}
}
}
class ttt4 implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(playerTurn){
TTT4.setText("X");
playerTurn = false;
wincheck.knop4C = 1;
round++;
}else{
TTT4.setText("O");
playerTurn = true;
wincheck.knop4C = 2;
round++;
}
}
}
class ttt5 implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(playerTurn){
TTT5.setText("X");
playerTurn = false;
wincheck.knop5C = 1;
round++;
}else{
TTT5.setText("O");
playerTurn = true;
wincheck.knop5C = 2;
round++;
}
}
}
class ttt6 implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(playerTurn){
TTT6.setText("X");
playerTurn = false;
wincheck.knop6C = 1;
round++;
}else{
TTT6.setText("O");
playerTurn = true;
wincheck.knop6C = 2;
round++;
}
}
}
class ttt7 implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(playerTurn){
TTT7.setText("X");
playerTurn = false;
wincheck.knop7C = 1;
round++;
}else{
TTT7.setText("O");
playerTurn = true;
wincheck.knop7C = 2;
round++;
}
}
}
class ttt8 implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(playerTurn){
TTT8.setText("X");
playerTurn = false;
wincheck.knop8C = 1;
round++;
}else{
TTT8.setText("O");
playerTurn = true;
wincheck.knop8C = 2;
round++;
}
}
}
class ttt9 implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(playerTurn){
TTT9.setText("X");
playerTurn = false;
wincheck.knop9C = 1;
round++;
}else{
TTT9.setText("O");
playerTurn = true;
wincheck.knop9C = 2;
round++;
}
}
}
}
и это моя победа или потерять контролер.
package tictactoe;
import javax.swing.JOptionPane;
public class wincheck {
static int knop1C;
static int knop2C;
static int knop3C;
static int knop4C;
static int knop5C;
static int knop6C;
static int knop7C;
static int knop8C;
static int knop9C;
public static void checkWin(){
if(knop1C == 1 && knop2C == 1 && knop3C == 1){
JOptionPane.showMessageDialog(null, "player 1 wins!");
System.exit(0);
}else if(knop4C == 1 && knop5C == 1 && knop6C == 1){
JOptionPane.showMessageDialog(null, "player 1 wins!");
System.exit(0);
}else if(knop7C == 1 && knop8C == 1 && knop9C == 1){
JOptionPane.showMessageDialog(null, "player 1 wins!");
System.exit(0);
}else if(knop1C == 1 && knop5C == 1 && knop9C == 1){
JOptionPane.showMessageDialog(null, "player 1 wins!");
System.exit(0);
}else if(knop3C == 1 && knop5C == 1 && knop7C == 1){
JOptionPane.showMessageDialog(null, "player 1 wins!");
System.exit(0);
}else if(knop1C == 1 && knop4C == 1 && knop7C == 1){
JOptionPane.showMessageDialog(null, "player 1 wins!");
System.exit(0);
}else if(knop2C == 1 && knop5C == 1 && knop8C == 1){
JOptionPane.showMessageDialog(null, "player 1 wins!");
System.exit(0);
}else if(knop3C == 1 && knop6C == 1 && knop9C == 1){
JOptionPane.showMessageDialog(null, "player 1 wins!");
System.exit(0);
}else if(knop1C == 2 && knop2C == 2 && knop3C == 2){
JOptionPane.showMessageDialog(null, "player 2 wins!");
System.exit(0);
}else if(knop4C == 2 && knop5C == 2 && knop6C == 2){
JOptionPane.showMessageDialog(null, "player 2 wins!");
System.exit(0);
}else if(knop7C == 2 && knop8C == 2 && knop9C == 2){
JOptionPane.showMessageDialog(null, "player 2 wins!");
System.exit(0);
}else if(knop1C == 2 && knop5C == 2 && knop9C == 2){
JOptionPane.showMessageDialog(null, "player 2 wins!");
System.exit(0);
}else if(knop3C == 2 && knop5C == 2 && knop7C == 2){
JOptionPane.showMessageDialog(null, "player 2 wins!");
System.exit(0);
}else if(knop1C == 2 && knop4C == 2 && knop7C == 2){
JOptionPane.showMessageDialog(null, "player 2 wins!");
System.exit(0);
}else if(knop2C == 2 && knop5C == 2 && knop8C == 2){
JOptionPane.showMessageDialog(null, "player 2 wins!");
System.exit(0);
}else if(knop3C == 2 && knop6C == 2 && knop9C == 2){
JOptionPane.showMessageDialog(null, "player 2 wins!");
System.exit(0);
}else if(round > 9){
JOptionPane.showMessageDialog(null, "Draw");
System.exit(0);
}
}
}
Может ли кто-нибудь помочь? Мне нужно это для школьного проекта.
(кстати, я нидерландский. Пожалуйста не против моей плохой грамматики)
Похоже, вы никогда не называете 'checkWin'. – resueman
1. Узнайте о массивах, а затем используйте их. Делая это, вы могли бы устранить 90% всего лишнего избыточного кода, чтобы сделать вашу программу намного легче изменить и отладить. 2. В вашем винчестере используются собственные переменные, ни одна из которых никак не связана с переменными GUI. 3. Лучше создать класс, совместимый с ООП, который выполняет проверку выигрыша и что GUI (или представление) может использовать. –
@resueman: даже если бы он это сделал, это ничего не принесло бы пользы. –