Yasin D. Yasin D. - 1 month ago 17
Java Question

TicTacToe If its a tie?

I'm a beginner in Java and I started to do a tic tac toe game. But I couldn't find how to do if it's a tie :(

I've tried to add "else" anywhere but when I did it, it comes up directly "It's a tie" window.

Here are my codes:

Main.java

/**
*
*/
package Game;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
* @author Yasin Demirci
*
*/
public class Main {
public static boolean playerTurn = true;
public static boolean playerWon = false;
public static boolean computerWon = false;

public static tictactoe board = new tictactoe();

public static void main(String[] args) {
if(board.isVisible() == false){
board.setVisible(true);
}
}
public static void checkforwin(){
if(board.button1.getText().equals("X")){
if(board.button4.getText().equals("X")){
if(board.button7.getText().equals("X")){
playerWon = true;
computerWon = false;
JOptionPane.showMessageDialog(new JFrame(), "Spelare 1 har vunnit!");
board.setVisible(false);
}
}
}

if(board.button1.getText().equals("X")){
if(board.button5.getText().equals("X")){
if(board.button9.getText().equals("X")){
playerWon = true;
computerWon = false;
JOptionPane.showMessageDialog(new JFrame(), "Spelare 1 har vunnit!");
}
}
}


if(board.button1.getText().equals("X")){
if(board.button2.getText().equals("X")){
if(board.button3.getText().equals("X")){
playerWon = true;
computerWon = false;
JOptionPane.showMessageDialog(new JFrame(), "Spelare 1 har vunnit!");
}
}
}


if(board.button3.getText().equals("X")){
if(board.button5.getText().equals("X")){
if(board.button7.getText().equals("X")){
playerWon = true;
computerWon = false;
JOptionPane.showMessageDialog(new JFrame(), "Spelare 1 har vunnit!");
}
}
}


if(board.button3.getText().equals("X")){
if(board.button6.getText().equals("X")){
if(board.button9.getText().equals("X")){
playerWon = true;
computerWon = false;
JOptionPane.showMessageDialog(new JFrame(), "Spelare 1 har vunnit!");
}
}
}


if(board.button7.getText().equals("X")){
if(board.button8.getText().equals("X")){
if(board.button9.getText().equals("X")){
playerWon = true;
computerWon = false;
JOptionPane.showMessageDialog(new JFrame(), "Spelare 1 har vunnit!");
}
}
}


if(board.button4.getText().equals("X")){
if(board.button5.getText().equals("X")){
if(board.button6.getText().equals("X")){
playerWon = true;
computerWon = false;
JOptionPane.showMessageDialog(new JFrame(), "Spelare 1 har vunnit!");
}
}
}


if(board.button2.getText().equals("X")){
if(board.button5.getText().equals("X")){
if(board.button8.getText().equals("X")){
playerWon = true;
computerWon = false;
JOptionPane.showMessageDialog(new JFrame(), "Spelare 1 har vunnit!");
}
}
}



if(board.button1.getText().equals("O")){
if(board.button4.getText().equals("O")){
if(board.button7.getText().equals("O")){
playerWon = false;
computerWon = true;
JOptionPane.showMessageDialog(new JFrame(), "Spelare 2 har vunnit!");
}
}
}

if(board.button1.getText().equals("O")){
if(board.button5.getText().equals("O")){
if(board.button9.getText().equals("O")){
playerWon = false;
computerWon = true;
JOptionPane.showMessageDialog(new JFrame(), "Spelare 2 har vunnit!");
}
}
}


if(board.button1.getText().equals("O")){
if(board.button2.getText().equals("O")){
if(board.button3.getText().equals("O")){
playerWon = false;
computerWon = true;
JOptionPane.showMessageDialog(new JFrame(), "Spelare 2 har vunnit!");
}
}
}


if(board.button3.getText().equals("O")){
if(board.button5.getText().equals("O")){
if(board.button7.getText().equals("O")){
playerWon = false;
computerWon = true;
JOptionPane.showMessageDialog(new JFrame(), "Spelare 2 har vunnit!");
}
}
}


if(board.button3.getText().equals("O")){
if(board.button6.getText().equals("O")){
if(board.button9.getText().equals("O")){
playerWon = false;
computerWon = true;
JOptionPane.showMessageDialog(new JFrame(), "Spelare 2 har vunnit!");
}
}
}


if(board.button7.getText().equals("O")){
if(board.button8.getText().equals("O")){
if(board.button9.getText().equals("O")){
playerWon = false;
computerWon = true;
JOptionPane.showMessageDialog(new JFrame(), "Spelare 2 har vunnit!");
}
}
}


if(board.button4.getText().equals("O")){
if(board.button5.getText().equals("O")){
if(board.button6.getText().equals("O")){
playerWon = false;
computerWon = true;
JOptionPane.showMessageDialog(new JFrame(), "Spelare 2 har vunnit!");
}
}
}


if(board.button2.getText().equals("O")){
if(board.button5.getText().equals("O")){
if(board.button8.getText().equals("O")){
playerWon = false;
computerWon = true;
JOptionPane.showMessageDialog(new JFrame(), "Spelare 2 har vunnit!");
}
}
}

if(playerWon == true || computerWon == true){
board.setVisible(false);
}
}
}

Answer

In your checkforwin() function, I would add a local boolean called tie initialized to true.

All the if statements occuring when either player wins would change that boolean to false.

Then add an if statement at the end to check if the boolean is still true, and if so, result in a tie.

if (tie == true)
{
  JOptionPane.showMessageDialog(new JFrame(), "Game was a tie!");
}
Comments