Question

Write a GUI based tic-tac-toe game for two players. Use code for the GUI and to...

Write a GUI based tic-tac-toe game for two players. Use code for the GUI and to set up event handling; include a Model.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Pseudo code for Tic Tac Toe Game :
1. Board is drawn first
2. Player X and Player O click the mouse on the empty grid of the board
3. Now Check at each click on the board
            3.1 Is there any three vertical grids with same Mark (i.e X or O)
                   if true
                             Player with that mark Wins and Stop
           3.2  Is there any three horizontal grids with same Mark (i.e X or O)
                   if true
                             Player with that mark Wins and Stop
          3.3   All grids filled   
                  if true
                             Declare result is stalemate
4.   Repeat step 2      


 
Java Code :
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;


public class TicTacToe extends Applet implements MouseListener 
{
    Frame f;
    int flag=2,n,m,i=0;
    char ch[]=new char[9];
    
    
    
    public TicTacToe()         
    {
        f=new Frame("Tic Tac Toe");
        f.setLayout(null);
        f.setVisible(true);
        f.setSize(600,600);
        f.addMouseListener(this); 
        for(i=0;i<9;i++)
        ch[i]='B';
    }
    public void mouseClicked(MouseEvent e)
    {
        Graphics g=f.getGraphics();
        g.drawLine(200,0,200,600);
        g.drawLine(400,0,400,600);
        g.drawLine(0,200,600,200);
        g.drawLine(0,400,600,400);
        flag--;
        int x=e.getX();
        int y=e.getY();
        if(flag==1)
        {
            if(x<200&&y<200){m=0;n=0;ch[0]='R';}
            if((x>200&&x<400)&&(y<200)){m=200;n=0;ch[1]='R';}
            if((x>400&&x<600)&&(y<200)){m=400;n=0;ch[2]='R';}
            if(x<200&&(y>200&&y<400)){m=0;n=200;ch[3]='R';}
            if((x>200&&x<400)&&(y>200&&y<400)){m=200;n=200;ch[4]='R';}
            if((x>400&&x<600)&&(y>200&&y<400)){m=400;n=200;ch[5]='R';}
            if(x<200&&(y>400&&y<600)){m=0;n=400;ch[6]='R';}
            if((x>200&&x<400)&&(y>400&&y<600)){m=200;n=400;ch[7]='R';}
            if((x>400&&x<600)&&(y>400&&y<600)){m=400;n=400;ch[8]='R';}
            g.setColor(Color.red);
            g.drawLine(m,n,m+199,n+199);
            g.drawLine(m+199,n,m,n+199);
        }
        
        if(flag==0)
        {
            
            if(x<200&&y<200){m=0;n=20;ch[0]='P';}
            if((x>200&&x<400)&&(y<200)){m=200;n=20;ch[1]='P';}
            if((x>400&&x<600)&&(y<200)){m=400;n=20;ch[2]='P';}
            if(x<200&&(y>200&&y<400)){m=0;n=200;ch[3]='P';}
            if((x>200&&x<400)&&(y>200&&y<400)){m=200;n=200;ch[4]='P';}
            if((x>400&&x<600)&&(y>200&&y<400)){m=400;n=200;ch[5]='P';}
            if(x<200&&(y>400&&y<600)){m=0;n=400;ch[6]='P';}
            if((x>200&&x<400)&&(y>400&&y<600)){m=200;n=400;ch[7]='P';}
            if((x>400&&x<600)&&(y>400&&y<600)){m=400;n=400;ch[8]='P';}
            g.setColor(Color.green);
            g.drawOval(m+10,n+10,169,169);
            //  g.drawLine(m,n,m+189,n+189);
            //  g.drawLine(m+199,n,m,n+199);
            flag=flag+2;
        }
        
        for(i=0;i<9;i++)   // for draw
        {
            if(ch[i]!='B')
            {
                if(i==8)
                draw();
            }
            else
            break;
        }
        
        for(i=0;i<3;i++)     //for vertical
        {
            //      System.out.print(ch[i]);
            if(ch[i]!='B')
            {
                if((ch[i+3]==ch[i])&&(ch[i+6]==ch[i]))
                win();
            }
        }
        for(i=0;i<7;i++)   //for horizontal
        {
            
            if(ch[i]!='B')
            {
                if((ch[i]==ch[i+1])&&(ch[i]==ch[i+2]))
                win();
                i=i+2;
            }
            else
            i=i+2;
        }
        
        if(ch[4]!='B')     //for diagonals
        {
            if(((ch[0]==ch[4])&&(ch[4]==ch[8]))||((ch[2]==ch[4])&&(ch[4]==ch[6])))
            win();
        }
        
        
    }
    public Frame win()
    {
        
        Frame m=new Frame("Result");
        Label l=new Label("you win");
        m.setLayout(null);
        m.add(l);
        l.setBounds(20,20,60,60);
        m.setVisible(true);
        m.setSize(100,100);
        return m;
    }
    
    public Frame draw()
    {
        Frame m=new Frame("Result");
        Label l1=new Label("Stalemate");
        m.setLayout(null);
        m.add(l1);
        l1.setBounds(20,20,60,60);
        m.setVisible(true);
        m.setSize(100,100);
        return m;
        
    }
    public void mouseReleased(MouseEvent e)
    {
        System.out.print("");
    }
    
    
    
    
    public void mouseEntered(MouseEvent e)
    
    {
        System.out.print("");
    }
    public void mouseExited(MouseEvent e)
    {
        System.out.print("");
    }
    public void mousePressed(MouseEvent e)
    {
        System.out.print("");
    }
    
    public static void main(String args [])
    {
        new TicTacToe();
    }
}
Add a comment
Know the answer?
Add Answer to:
Write a GUI based tic-tac-toe game for two players. Use code for the GUI and to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (Game: play a tic-tac-toe game) In a game of tic-tac-toe, two players take turns marking an...

    (Game: play a tic-tac-toe game) In a game of tic-tac-toe, two players take turns marking an available cell in a grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A draw (no winner) occurs when all the cells in the grid have been filled with tokens and neither player has achieved a win. Create...

  • 18. Tic-Tac-Toe Game rite a program that allows two players to play a game of tic-tac-toe. Use di...

    18. Tic-Tac-Toe Game rite a program that allows two players to play a game of tic-tac-toe. Use dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Write . Displays the contents of the board array. . Allows player 1 to select a location on the board for an X. The program should ask the...

  • Write a program to Simulate a game of tic tac toe in c#

    Write a program to Simulate a game of tic tac toe. A game of tic tac toe has two players. A Player class is required to store /represent information about each player. The UML diagram is given below.Player-name: string-symbol :charPlayer (name:string,symbol:char)getName():stringgetSymbol():chargetInfo():string The tic tac toe board will be represented by a two dimensional array of size 3 by 3 characters. At the start of the game each cell is empty (must be set to the underscore character ‘_’). Program flow:1)    ...

  • (Tic-Tac-Toe) Create a class Tic-Tac-Toe that will enable you to write a program to play Tic-Tac-Toe....

    (Tic-Tac-Toe) Create a class Tic-Tac-Toe that will enable you to write a program to play Tic-Tac-Toe. The class contains a private 3-by-3 two-dimensional array. Use an enumeration to represent the value in each cell of the array. The enumeration’s constants should be named X, O and EMPTY (for a position that does not contain an X or an O). The constructor should initialize the board elements to EMPTY. Allow two human players. Wherever the first player moves, place an X...

  • 1. Use Turtle Graphics to create a tic tac toe game in Python. Write a Python program that allows for one player vs computer to play tic tac toe game, without using turtle.turtle

    1. Use Turtle Graphics to create a tic tac toe game in Python. Write a Python program that allows for one player vs computer to play tic tac toe game, without using turtle.turtle

  • PYTHON Exercise 2. Tic-Tac-Toe In this exercise we are going to create a Tic-Tac-Toe game. 1....

    PYTHON Exercise 2. Tic-Tac-Toe In this exercise we are going to create a Tic-Tac-Toe game. 1. Create the data structure – Nine slots that can each contain an X, an O, or a blank. – To represent the board with a dictionary, you can assign each slot a string-value key. – String values in the key-value pair to represent what’s in each slot on the board: ■ 'X' ■ 'O' ■ ‘ ‘ 2. Create a function to print the...

  • Tic Tac Toe Game: Help, please. Design and implement a console based Tic Tac Toe game....

    Tic Tac Toe Game: Help, please. Design and implement a console based Tic Tac Toe game. The objective of this project is to demonstrate your understanding of various programming concepts including Object Oriented Programming (OOP) and design. Tic Tac Toe is a two player game. In your implementation one opponent will be a human player and the other a computer player. ? The game is played on a 3 x 3 game board. ? The first player is known as...

  • CSII Java plz GUI Implementation Implement a tic-tac-toe game via GUI. Your game must have to...

    CSII Java plz GUI Implementation Implement a tic-tac-toe game via GUI. Your game must have to panels, one display the game board for the user to play and the other is a control panel showing the user with options to pay a new game or quit. At any moment, only one panel is shown taking the entire window/frame. Initially, the game boar shown first, and the control panel is shown only when the user completes a game.

  • How to write a (c++ program) tic tac toe code where its the computer against a...

    How to write a (c++ program) tic tac toe code where its the computer against a human. and the computer always wins. a basic c++ code program using gaming theory arrays <stdio.h> computer plays tic tac toe against a user. computer always when or the game is a draw.

  • In a game of Tic Tac Toe, two players take turns making an available cell in...

    In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT