Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Tic Tac Toe Game Help!  (Read 754 times)
0 Members and 1 Guest are viewing this topic.
Offline iamscott06

Innocent Bystander





« Posted 2006-01-08 20:23:32 »

Hey im sort of new to java. i took a class in school called JAVA and it talked about the basics. And know that the class is over i wanted to create something on my own and i decided to try and make tic tac toe the game.. it started out going good but once i made the board and set the turns i got stuck  Embarrassed. I can't figure out how to tell which player won the game and i also want to cheat proof my game , you can click the box twice and make it go from an x to an o or vice versa. Your help would be greatly appreciated and if you could could you explain what your doing so i can do it right the next time i try. Thanks for your help!

Scott.

[size=7pt]

import javax.swing.*;
import BreezySwing.*;
import java.awt.*;
import java.awt.event.*;

public class Tictactoe extends GBFrame{
   //Declare varialbe for the window objects
   private JLabel      title;

   private JButton     topleft;
   private JButton      topmiddle;
   private JButton       topright;
   private JButton      middleleft;
   private JButton      middlemiddle;
   private JButton      middleright;
   private JButton      bottomleft;
   private JButton      bottommiddle;
   private JButton      bottomright;
   private JButton      newgame;
   private int x, y=0;

   
   //constructor
   public Tictactoe(){
      //instantiate and add window objects to the window
      title = addLabel      ("Tic Tac Toe" ,1,2,1,1);

      
      topleft          = addButton     (" "   ,2,1,1,1);
      topmiddle      = addButton       (" "   ,2,2,1,1);
      topright      = addButton       (" "   ,2,3,1,1);
      middleleft      = addButton       (" "   ,3,1,1,1);
      middlemiddle    = addButton       (" "   ,3,2,1,1);
      middleright    = addButton       (" "   ,3,3,1,1);
      bottomleft      = addButton       (" "   ,4,1,1,1);
      bottommiddle    = addButton       (" "   ,4,2,1,1);
      bottomright    = addButton       (" "   ,4,3,1,1);
      newgame         = addButtom       ("New Game"  , 5,2,1,1);
   }
   
   //respond to button click events
   
   public void buttonClicked (JButton buttonObj){

   if (y=o){
      y=1;
   }
      
   if (buttonObj == topleft){
      if ((x%2)==o){
         topleft.setText("X");
      }else{
         topleft.setText("O");
      }
               
      x+=1;
   }               

   if (buttonObj == topmiddle){
      if ((x%2)==o){
         topmiddle.setText("X");
      }else{
         topmiddle.setText("O");
      }
               
      x+=1;
   }   

   if (buttonObj == topright){
      if ((x%2)==o){
         topright.setText("X");
      }else{
         topright.setText("O");
      }
               
      x+=1;
   }
   
      if (buttonObj == middleleft){
      if ((x%2)==o){
         middleleft.setText("X");
      }else{
         middleleft.setText("O");
      }
               
      x+=1;
   }
   
      if (buttonObj == middlemiddle){
      if ((x%2)==o){
         middlemiddle.setText("X");
      }else{
         middlemiddle.setText("O");
      }
               
      x+=1;
   }
   
      if (buttonObj == middleright){
      if ((x%2)==o){
         middleright.setText("X");
      }else{
         middleright.setText("O");
      }
               
      x+=1;
   }

   if (buttonObj == bottomleft){
      if ((x%2)==o){
         bottomleft.setText("X");
      }else{
         bottomleft.setText("O");
      }
               
      x+=1;
   }
   
      if (buttonObj == bottommiddle){
      if ((x%2)==o){
         bottommiddle.setText("X");
      }else{
         bottommiddle.setText("O");
      }
               
      x+=1;
   }
   
      if (buttonObj == bottomright){
      if ((x%2)==o){
         bottomright.setText("X");
      }else{
         bottomright.setText("O");
      }
               
      x+=1;
   }

   if (buttonObj == newgame){
      topleft.setText(" ");
      topmiddle.setText(" ");
      topright.setText(" ");
      middleleft.setText(" ");
      middlemiddle.setText(" ");
      middleright.setText(" ");
      bottomleft.setText(" ");
      bottommiddle.setText(" ");
      bottomright.setText(" ");
      
      x = 0;
   }
      
}
      public static void main (String[] args){
            
      Tictactoe theGUI = new Tictactoe();
      theGUI.setSize (275, 205);
      theGUI.setLookAndFeel("Metal");
      theGUI.setVisible (true);
   }

   
}

[/size]
Offline Mr_Light

Senior Member


Medals: 1


shiny.


« Reply #1 - Posted 2006-01-08 20:27:46 »

they didn't happen to talk about sequence diagram's did they?  Kiss

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #2 - Posted 2006-01-08 23:08:06 »

You can replace most of your code with

buttonObj.setText( (x++ & 1 == 0) ? "X" : "O");

I.e. what is the pointof all those IF statements when you can use buttonObj directly?

Anyway.. the problems are:
- you aren't checking if the exiting "square" is empty before setting the new contents
         if(" ".equals(buttonObj.getText())) { allow the move }

- you aren't checking the game board for a win after each play
  simply check the row, column  and  diagonal(s) that contain the last clicked button to see if any of them are all filled with the same symbol


Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

BurntPizza (22 views)
2014-09-19 03:14:18

Dwinin (35 views)
2014-09-12 09:08:26

Norakomi (62 views)
2014-09-10 13:57:51

TehJavaDev (90 views)
2014-09-10 06:39:09

Tekkerue (44 views)
2014-09-09 02:24:56

mitcheeb (65 views)
2014-09-08 06:06:29

BurntPizza (48 views)
2014-09-07 01:13:42

Longarmx (35 views)
2014-09-07 01:12:14

Longarmx (40 views)
2014-09-07 01:11:22

Longarmx (37 views)
2014-09-07 01:10:19
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!