Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (602)
Games in Android Showcase (171)
games submitted by our members
Games in WIP (650)
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 895 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!



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;

   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){
   if (buttonObj == topleft){
      if ((x%2)==o){

   if (buttonObj == topmiddle){
      if ((x%2)==o){

   if (buttonObj == topright){
      if ((x%2)==o){
      if (buttonObj == middleleft){
      if ((x%2)==o){
      if (buttonObj == middlemiddle){
      if ((x%2)==o){
      if (buttonObj == middleright){
      if ((x%2)==o){

   if (buttonObj == bottomleft){
      if ((x%2)==o){
      if (buttonObj == bottommiddle){
      if ((x%2)==o){
      if (buttonObj == bottomright){
      if ((x%2)==o){

   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.setVisible (true);


Offline Mr_Light

Senior Devvie

Medals: 1


« 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

Exp: 12 years

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.

Jesse (16 views)
2015-07-29 04:35:27

Riven (37 views)
2015-07-27 16:38:00

Riven (19 views)
2015-07-27 15:35:20

Riven (22 views)
2015-07-27 12:26:13

Riven (12 views)
2015-07-27 12:23:39

BurntPizza (30 views)
2015-07-25 00:14:37

BurntPizza (42 views)
2015-07-24 22:06:39

BurntPizza (24 views)
2015-07-24 06:06:53

NoxInc (31 views)
2015-07-22 22:16:53

NoxInc (20 views)
2015-07-22 22:13:39
List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21 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‑
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!