Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (541)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
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 help  (Read 3541 times)
0 Members and 1 Guest are viewing this topic.
Offline ahmedkl

Junior Newbie





« Posted 2009-11-21 15:58:09 »

Hello people I need some help with making tic tac toe , i have made it with buttons , but now i want to make it without buttons , like I place my own tic tac toe grid on the frame and then when user clicks on a certain block then it should but circle or cross on that place , I know mouseListener will be used , but i want to know how could i possibly do it on my own image?
Offline indexunknown

Junior Devvie





« Reply #1 - Posted 2009-11-21 16:08:56 »

You could do it with Rectangles

class MyButton{
Rectangle bounds = {x,y,width,height};
String name;
}

u could put them all into array too.

ArrayList<MyButton> buttons;

in event handler:
mousePressed(Event e){

for(MyButton t: buttons){
    if(t.bounds.contains(e.getX(),e.getY())){
        system out println t.name pressed
   }
}
Offline ahmedkl

Junior Newbie





« Reply #2 - Posted 2009-11-21 16:50:59 »

I don't get the rectangle thing you told , I do know about the mouseListeners and stuff  Undecided
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline indexunknown

Junior Devvie





« Reply #3 - Posted 2009-11-21 17:10:39 »

The Rectangle is for catching a mouse click , you could also add a picture for MyButton:

class MyButton{
Rectangle bounds = {x,y,width,height};
String name;
BufferedImage circle;
BufferedImage cross;

int picture = 0;

public MyButton(x,y,w,h){
    here set object members

    URL url = this.getClass().getResource("circle.png");
    circle= ImageIO.read(url);
    url = this.getClass().getResource("cross.png");
    cross= ImageIO.read(url);
}

public void clicked(){
if picture == 0 then change to 1 or 2 depending if cross or circle
 
}

public void draw(Graphics g){

if(picture == 0){
    draw white square
}
if( picture == 1)  {
    g.drawImage circle using bounds as location
}
if( picture == 2)  {
    g.drawImage cross using bounds as location
}
}
}


in game create the array of buttons if one is clicked do with it what u need in MyButton clicked method.
Online Riven
« League of Dukes »

« JGO Overlord »


Medals: 848
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2009-11-21 17:14:19 »

First, I admire you took a step back from your RTS plans and started with tic-tac-toe.

Rectangle is a class inside the java.awt.* package. Use the Javadocs to see how to use it. List can be found in java.util.*, and the example of 'indexunknown' shows you how to use the enhanced for loop, you can switch to a simple for loop, or use an iterator with a while loop.


I'm really not trying to be lame here, but searching for stuff with Google is often much faster (a few seconds) to yield an answer than using this forum (a few hours).

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #5 - Posted 2009-11-21 17:32:28 »

Riven but sometimes google doesnt have the answers Wink.
Online Riven
« League of Dukes »

« JGO Overlord »


Medals: 848
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #6 - Posted 2009-11-21 19:54:22 »

Google knows everything about tic tac toe.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Offline ahmedkl

Junior Newbie





« Reply #7 - Posted 2009-11-22 00:59:44 »

I have searched on google but didn't find any custom made java tic tac toe , all of em are made with buttons Sad , i too have made one with buttons but i want to do it the other way now  persecutioncomplex
Online Riven
« League of Dukes »

« JGO Overlord »


Medals: 848
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #8 - Posted 2009-11-22 11:00:30 »

In my first post, I mean that Google knows everything about Rectangle too, as you asked about that.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Offline zoto

Senior Devvie


Medals: 4



« Reply #9 - Posted 2009-11-22 14:32:35 »

I'm guessing by buttons you mean the keyboard. If you already made the game using keyboard input and I'm guessing console output then all you really need to change are those 2 things, the logic should run the same either way.
For the drawing a simple passive rendering should work great. Look into overwriting java.awt.Panel or javax.swing.JPanel. For input just use code similar to what indexunknown posted for your grid cells then just keep a list of the (9 or whatever you need) cells and whenever the mouse is pressed you can loop through checking each cell untill you find the cell that was clicked on or the user didn't click a cell. If the user did click a cell use the logic you already wrote for the console version.
The JavaDoc is a great way to learn about Java.
http://java.sun.com/javase/6/docs/api/
The top left box shows the package (such as java.awt) the bottom left panel shows the actual classes and stuff in that package (like Rectangle).
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #10 - Posted 2009-11-22 14:52:35 »

by buttons he means taht he used java.awt.JButton (atleast I thinkt hat is the location)
Offline ahmedkl

Junior Newbie





« Reply #11 - Posted 2009-11-23 12:54:01 »

ok here's my code so far the problem now i have is to choose which turn  is it of the cross or circle and also how to make checks that this player has won or not

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class myTicTacToe extends MouseAdapter{

   public myTicTacToe(){
   
   jf=new JFrame("Tic Tac Toe");
   jf.setVisible(true);
   jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
   jf.setSize(500,500);
   jf.setResizable(false);
   jf.setLayout(null);
   ii=new ImageIcon("F:\\ttt\\frame.gif");
   c=new ImageIcon("F:\\ttt\\circle.gif");
   cx=new ImageIcon("F:\\ttt\\cross.gif");
   jl=new JLabel(ii);
   circle=new JLabel(c);
   cross=new JLabel(cx);
   jpi=new JPanel();  // panel
   
   jf.addMouseListener(this);
   test=new JLabel();
   jf.add(test);
   test.setBounds(20,20,50,50);
   }


   public void init(){
   jf.add(jl);
   jf.add(jpi);
     
   test.setBounds(50,10,100,10);
   jpi.setBounds(50,75,290,290);
   jpi.setLayout(null);
   jl.setBounds(50,75,300,300);
   
   for(int i=0;i<9;i++){
      box[i]=new JLabel();
      jpi.add(box[i]);
      box[i].addMouseListener(this);
      }
   
   }

   
   
   public void mousePressed(MouseEvent me){
      int x=me.getX();
      int y=me.getY();
      test.setText(x+"  "+y);
      if( (x>76 && x<151) && (y>131 && y<203) ){
      box[0].setBounds(27,27,70,70);
      box[0].setIcon(new ImageIcon("circle.gif"));  
      }
     
      else if( (x>169 && x<242) && (y>131 && y<203) ){
      box[1].setBounds(118,27,70,70);
      box[1].setIcon(new ImageIcon("cross.gif"));  
      System.out.println("2nd");
      }
     
      else if( (x>258 && x<333) && (y>131 && y<203) ){
      box[2].setBounds(210,27,70,70);
      box[2].setIcon(new ImageIcon("circle.gif"));  
      System.out.println("3rd");
      }
     
      else if( (x>76 && x<151) && (y>222 && y<288) ){
      box[3].setBounds(27,115,70,70);
      box[3].setIcon(new ImageIcon("cross.gif"));  
      System.out.println("4");
      }
     
      else if( (x>169 && x<242) && (y>222 && y<288) ){
      box[4].setBounds(118,115,70,70);
      box[4].setIcon(new ImageIcon("cross.gif"));  
      System.out.println("5");
      }
     
      else if( (x>258 && x<333) && (y>222 && y<288) ){
      box[5].setBounds(210,115,70,70);
      box[5].setIcon(new ImageIcon("cross.gif"));  
      System.out.println("6");
      }
     
      else if( (x>76 && x<151) && (y>308 && y<381) ){
      box[6].setBounds(27,205,70,70);
      box[6].setIcon(new ImageIcon("cross.gif"));  
      System.out.println("7");
      }
     
      else if( (x>169 && x<242) && (y>308 && y<381) ){
      box[7].setBounds(118,205,70,70);
      box[7].setIcon(new ImageIcon("cross.gif"));  
      System.out.println("8");
      }
     
      else if( (x>258 && x<333) && (y>308 && y<381) ){
      box[8].setBounds(210,205,70,70);
      box[8].setIcon(new ImageIcon("cross.gif"));  
      System.out.println("9");
      }
     
     
   }
   

private JFrame jf;
private String p1;
private String p2;
private ImageIcon ii,c,cx;
private JLabel jl;
private JLabel circle,test;
private JLabel cross;
private JPanel jpi;
private JLabel box[]=new JLabel[9];
private int count=0;
private boolean player=false;
}

class myMain{

   public static void main(String arg[]){
   
   myTicTacToe ttt=new myTicTacToe();
   ttt.init();
   
   }
}
Online Riven
« League of Dukes »

« JGO Overlord »


Medals: 848
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #12 - Posted 2009-11-23 14:38:54 »

You basically do not have any game code, only a slightly intelligent user-interface class.

Simplistic game code could look like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
class Player
{
   Game game;

   void pick(int x, int y)
   {
       game.playerPicks(this, x, y);
   }
}

class Game
{
   Player player1, player2;
   Player playing;

   Square[][] squares = new Square[3][3];

   void playerPicks(Player p, int x, int y)
   {
      if(p != playing)
         throw new IllegalStateException("shh, it was not your turn");

      Square square = squares[x][y];

      if(square.owner != null)
         throw new IllegalStateException("this square is already taken");
      square.owner = p;

      if(playing == player1)
         player = player2;
      else if(playing == player2)
         player = player1;

      // check for winning move

      view.repaint();
   }
}

class Square
{
    int x, y, w, h;
    Player owner;
}


class View
{
   void paint(Graphics g)
   {
      for(int x=0; x<3; x++)
      {
         for(int y=0; y<3; y++)
         {
              Square square = game.squares[x][y];
              Player owner = square.owner;

              // draw the square at X,Y, with the icon for the player, if any
         }
      }
   }

   void mouseClicked(...)
   {
      Player player = game.playing;

      for(int x=0; x<3; x++)
      {
         for(int y=0; y<3; y++)
         {
              Square square = game.squares[x][y];

               if(square.x >= xMouse && square.x + square.w <= xMouse)
                  if(square.y >= yMouse && square.y + square.h <= yMouse)
                     player.pick(x, y);
         }
      }
   }
}


If anybody is going to suggest improvements to this code... well, don't be Captain Obvious, this is code to get him started.


Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Offline SimonH
« Reply #13 - Posted 2009-11-23 16:31:37 »

Or maybe this...

People make games and games make people
Online Riven
« League of Dukes »

« JGO Overlord »


Medals: 848
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #14 - Posted 2009-11-23 16:53:00 »

Or maybe this...

 Undecided

Do you realise how tempting it will be for him to copy/paste??

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #15 - Posted 2009-11-23 17:34:00 »

Undecided

Do you realise how tempting it will be for him to copy/paste??

yeah, me learning how ot make tic tac to was ruined by finding the exact source code for the game, so I didnt have to findout how to do it.
Offline ahmedkl

Junior Newbie





« Reply #16 - Posted 2009-11-23 17:36:22 »

Riven thanks , your code did give me an idea about neat coding that is to be used in games , and I don't copy source codes so don't worry about it Smiley.
And can you name any good books related to java game programming ?

Thanks
Online Riven
« League of Dukes »

« JGO Overlord »


Medals: 848
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #17 - Posted 2009-11-23 17:40:32 »

Riven thanks , your code did give me an idea about neat coding that is to be used in games , and I don't copy source codes so don't worry about it Smiley.
And can you name any good books related to java game programming ?

Thanks

I don't buy books... maybe somebody else can jump in.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Online Riven
« League of Dukes »

« JGO Overlord »


Medals: 848
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #18 - Posted 2009-11-23 17:43:17 »

yeah, me learning how ot make tic tac to was ruined by finding the exact source code for the game, so I didnt have to findout how to do it.

That's your own fault.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #19 - Posted 2009-11-23 18:42:01 »

I know, but in my defense,

I googled, how to make a grid of buttons.

I get a guys asking how to do that exact thing, great!

only problem, the source given is a game of tic tac to, I picked the lesser of two evils

A)Dont finish the project
B)Look at the code
Offline SimonH
« Reply #20 - Posted 2009-11-23 23:52:29 »

Do you realise how tempting it will be for him to copy/paste??
That would be his loss. Anyway, a good tutor would spot it from a mile away!

People make games and games make people
Online Riven
« League of Dukes »

« JGO Overlord »


Medals: 848
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #21 - Posted 2009-11-23 23:55:30 »

That would be his loss. Anyway, a good tutor would spot it from a mile away!

So you give him sourcecode, and if he'd use it, it would be his loss. Go figure.

Besides that, it must be once of the worst tic-tac-toe implementations ever... somebody is addicted to bitwise operations and nested switches.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Offline SimonH
« Reply #22 - Posted 2009-11-24 00:15:40 »

So you give him sourcecode, and if he'd use it, it would be his loss. Go figure.
*sigh* Why is everyone kicking me today? Have I got a sign on my back or something?  persecutioncomplex
I can't see the harm - I learned a lot from looking at other people's code and working out how they did it - didn't you?

People make games and games make people
Online Riven
« League of Dukes »

« JGO Overlord »


Medals: 848
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #23 - Posted 2009-11-24 00:44:50 »

Not meant like that. Just got a bit irritated when I spent quite some time on my reply, and then I see a one-line post, that's IMHO totally how not to write a game.

And to answer your question, I hardly every look at other peoples code. I'm not really a teamplayer, but I guess that was obvious, considering my last post persecutioncomplex

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
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.

Mr.CodeIt (24 views)
2014-12-23 03:34:11

rwatson462 (55 views)
2014-12-15 09:26:44

Mr.CodeIt (46 views)
2014-12-14 19:50:38

BurntPizza (91 views)
2014-12-09 22:41:13

BurntPizza (113 views)
2014-12-08 04:46:31

JscottyBieshaar (83 views)
2014-12-05 12:39:02

SHC (92 views)
2014-12-03 16:27:13

CopyableCougar4 (102 views)
2014-11-29 21:32:03

toopeicgaming1999 (161 views)
2014-11-26 15:22:04

toopeicgaming1999 (164 views)
2014-11-26 15:20:36
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!