Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

tic tac toe game code using java JFrame, JPanel,JButton

+10 votes
asked Apr 15, 2023 by Parveen Dhaliwal (290 points)

2 Answers

+5 votes
answered Apr 15, 2023 by Peter Minarik (86,240 points)
Where is the code? What is your question?

Share your code, what is the issue, how to reproduce it, and people will be happy to help you smooth out the wrinkles with your program.
0 votes
answered Apr 17, 2023 by Anmol Upadhyay (220 points)
#include <iostream>

#include <cstring>

#include <algorithm>

#include <ctime>

using namespace std;

#define compareBoxes(box1, box2, box3) ((board[box1] == board[box2]) && (board[box2] == board[box3]) && (board[box1] != 0)) //Checkes if three items are the same, and makes sure they're not 0's.

#define numberToLetter(x) ((x > 0) ? (x == 1) ? 'X' : 'O' : ' ') //Takes the number and turns it into the letter or space.

int getWinner(int board[9]) {

//Finds winner of game, if there is no winner, returns 0.

int winner = 0;

for (int x = 0; x < 3; x++) {

if (compareBoxes(3*x, 3*x+1, 3*x+2)) { //Chekcs rows.

winner = board[3*x];

break;

} else if (compareBoxes(x, x+3, x+6)) { //Checks columns.

winner = board[x];

break;

} else if (compareBoxes(2*x, 4, 8-2*x) && (x < 2)) { //Checks diagonals. Doesn't check if x == 2.

winner = board[4];

break;

}

}

return winner;

}

bool gameOver(int board[9]){

//Checks if game is over, and announces who won, or if it was a tie.

int winner = getWinner(board);

if (winner > 0) {

cout << numberToLetter(winner) << " wins!"<< endl;

return true;

}

for (int x = 0; x < 9; x++) {

if (board[x] == 0) return false;

}

cout << "Tie!\n\n";

return true;

}

int willWin(int board[9], int player) {

//Checks if a given player could win in the next plank.

for (int x = 0; x < 9; x++) {

int tempBoard[9];

memcpy(tempBoard, board, 36);

if (board[x] > 0) continue;

tempBoard[x] = player;

if(getWinner(tempBoard) == player) return x;

}

return -1;

}

int exceptionalCase(int board[9]) {

//Finds bords that are exceptions to how the algorithm works.

int cases[2][9] = {{1,0,0,0,2,0,0,0,1}, {0,1,0,1,2,0,0,0,0}}; //Boards that don't work with algorithm.

int answers[2][4] = {{3,3,3,3}, {2,8,6,0}};

int rotatedBoard[9] = {6,3,0,7,4,1,8,5,2};

int newBoard[9];

int tempBoard[9];

for(int x = 0; x < 9; x++) {

newBoard[x] = board[x];

}

for (int caseIndex = 0; caseIndex < 2; caseIndex++) {

for(int rotation = 0; rotation < 4; rotation++) {

for (int x = 0; x < 9; x++)

tempBoard[x] = newBoard[x];

int match = 0;

//Rotates board so it works with different versions of the same board.

for (int box = 0; box < 9; box++) {

newBoard[box] = tempBoard[rotatedBoard[box]];

}

for (int x = 0; x < 9; x++) {

if (newBoard[x] == cases[caseIndex][x]) match++;

else break;

}

if (match == 9) return answers[caseIndex][rotation];

}

}

return -1;

}

int getSpace(int board[9], int spaces[4]) {

//Gets a random corner or side that's not taken.

bool isSpaceEmpty = false;

int y;

for (int x = 0; x < 4; x++) {

if (board[spaces[x]] == 0) {

isSpaceEmpty = true;

break;

}

}

if (isSpaceEmpty) {

do {

y = rand() % 4;

} while (board[spaces[y]] != 0);

return spaces[y];

}

return -1;

}

void outputBoard(int board[9]) {

for(int line = 0; line < 3; line++){

for (int box = 0; box < 3; box++) {

cout << numberToLetter(board[3*line+box]) << ((box < 2) ? '|' : '\n');

}

cout << ((line < 2) ? "-----\n" : "\n");

}

}

int main(){

int board[9] = {0,0,0,0,0,0,0,0,0}; //Starts empty board.

int possibleWinner;

int move;

bool isInvalid;

string moveString;

srand((int) time(0));

int corners[4] = {0,2,6,8};

int sides[4] = {1,3,5,7};

cout << "1|2|3\n-----\n4|5|6\n-----\n7|8|9\n\n";

while (true) {

//Player X decides what move they'll do.

do {

cout << "X: ";

getline(cin, moveString);

move = moveString[0] - '1';

if (move > 8 || move < 0 || board[move] != 0) {

cout << "Invalid input" << endl;

isInvalid = true;

} else {

board[move] = 1;

isInvalid = false;

cout << endl;

}

} while (isInvalid);

//Decides whether or not the game continues.

if (gameOver(board) > 0) {

outputBoard(board);

break;

}

//Player O decides which move they'll do.

bool good = false;

for (int x = 2; x > 0; x--){

possibleWinner = willWin(board, x);

if (possibleWinner != -1) {

board[possibleWinner] = 2;

good = true;

break;

}

}

if (good);

else if (board[4] == 0) board[4] = 2; //Middle.

else if (exceptionalCase(board) > -1) board[exceptionalCase(board)] = 2; //Exception boards.

else if (getSpace(board, corners) != -1) board[getSpace(board, corners)] = 2; //Corners

else board[getSpace(board, sides)] = 2; //Sides

//Prints the board to the screen.

outputBoard(board);

//Decides whether or not the game continues.

if(gameOver(board)) break;

}

return 0;

}
commented Aug 3, 2023 by 08_Saptarshi Chatterjee (100 points)
edited Aug 9, 2023 by 08_Saptarshi Chatterjee
echo "Player 1:";
$str=readline();
echo "Player 2:";
$str2=readline();
echo "Enter no of rows:";
$n=readline();
$a=array();
for ($i = 0; $i < $n; $i++) {
    for ($j = 0; $j < $n; $j++) {
        $a[$i][$j]=0;
    }
}
$attempts=0;
for ($i = 1; $i <=($n*$n); $i++) {
    if($i%2!=0){
        show($n,$a);
        echo "Enter position of ".$str.":";
        $b=readline();
        $c=readline();
        if($b<$n && $c<$n){
           if($a[$b][$c]==0){
              $a[$b][$c]=1;
              $count=0;
              for ($x = 0; $x < $n; $x++) {
                   if($a[$b][$x]==1){
                      $count++;
                    }
                }
                if($count!=$n){
                   $count=0;
                   for ($x = 0; $x < $n; $x++) {
                      if($a[$x][$c]==1){
                          $count++;
                        }
                    }
                }
                if($count!=$n){
                  if($b==$c){
                     $count=0;
                     for ($x = 0; $x <$n; $x++) {
                         for ($y = 0; $y < $n; $y++) {
                             if($x==$y){
                                 if($a[$x][$y]==1){
                                     $count++;
                                    }
                                }
                            }
                        }
                    }else if($b+$c==$n-1){
                      $count=0;
                      for ($x = 0; $x <$n; $x++) {
                          for ($y = 0; $y < $n; $y++) {
                              if($x+$y==$n-1){
                                 if($a[$x][$y]==1){
                                      $count++;
                                    }
                                }
                            }
                        }
                    }
                }
                if($count==$n){
                   echo $str." wins";
                   echo "\n";
                   show($n,$a);
                   break;
                }
            }else{
               echo "Position already taken. ";
               show($n,$a);
               break;
            }
        }else{
            echo "Invalid Input.";
            break;
        }
    }else{
        show($n,$a);
        echo "Enter position of ".$str2.":";
        $b=readline();
        $c=readline();
        if($b<$n && $c<$n){
           if($a[$b][$c]==0){
              $a[$b][$c]=2;
              $count2=0;
              for ($x = 0; $x < $n; $x++) {
                  if($a[$b][$x]==2){
                     $count2++;
                    }
                }
              if($count2!=$n){
                  $count2=0;
                  for ($x = 0; $x < $n; $x++) {
                     if($a[$x][$c]==2){
                         $count2++;
                        }
                    }
                }
              if($count2!=$n){
                 if($b==$c){
                     $count2=0;
                     for ($x = 0; $x <$n; $x++) {
                          for ($y = 0; $y < $n; $y++) {
                             if($x==$y){
                                 if($a[$x][$y]==2){
                                     $count2++;
                                    }
                                }
                            }
                        }
                    }else if($b+$c==$n-1){
                       $count2=0;
                       for ($x = 0; $x <$n; $x++) {
                          for ($y = 0; $y < $n; $y++) {
                             if($x+$y==$n-1){
                                 if($a[$x][$y]==2){
                                     $count2++;
                                    }
                                }
                            }
                        }    
                    }
                }
                if($count2==$n){
                  echo $str2." wins";
                  echo "\n";
                  show($n,$a);
                  break;
                }
            }else{
                  echo "Position already taken. ";
                  show($n,$a);
                  break;
                }
        }else{
            echo "Invalid input.";
            break;
        }
    }
    $attempts++;
}
if($attempts==$n*$n){
    echo "Match draw.";
    show($n,$a);
}
function show($x,$arr){
for ($i = 0; $i < $x; $i++) {
    for ($j = 0; $j < $x; $j++) {
        echo $arr[$i][$j]." ";
    }
    echo "\n";
}
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...