Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,407 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,471 people online right now. Registration is fast and FREE... Join Now!




Tic Tac Toe

 
Reply to this topicStart new topic

Tic Tac Toe, Topics merged... avoid double posting

BradburyRob
15 Oct, 2008 - 07:47 AM
Post #1

New D.I.C Head
*

Joined: 14 Oct, 2008
Posts: 5


My Contributions
Hi,

/I am trying to implement random numbers in this game but I am pretty brain dead as to how to do that. I was able to implement random numbers on a rock paper scissors game. I started with a 2dArray approach to create a 3 X 3 layout on the applet. Now I just changed the size of the applet so the single D array can fit nicely.


//Right now I need to have the PC make a move on the game, then follow that move with a human user then the PC again and so on, until either one has won or 9 moves have been made on the board.

//How do I make the PC make a move? Thank you,
________________________________________________________________________________
_______________

java
import java.util.*;
import java.util.Random;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JTicTacToe extends JApplet implements ActionListener
{



JLabel someText = new JLabel(" Enter the number of expected guest for the event: ");



JTextField firstNum = new JTextField(15);

JLabel test_two = new JLabel("");

Container test = getContentPane();

String letter4User = "O";

String letterForPC = "X";



String[] dummyInput = {" ", " ", " ", " ", " ", " ", " ", " ", " "};

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

JButton[] TicTac = new JButton[dummyInput.length];

int random = ((int) (Math.random() * 100) % 9 + 1);

int count = 0;

int y;






Font convertion = new Font ("Bookman Old Style", Font.ITALIC, 13);




public void init()

{



test.setLayout(new FlowLayout());


firstNum.addActionListener(this);


for(int y = 0; y < dummyInput.length; y++)

{
TicTac[y] = new JButton(dummyInput[y]);

test.add(TicTac[y]);

TicTac[y].addActionListener(this);

}





}

public void actionPerformed(ActionEvent r)



{


Object source = r.getSource();

y = verify[y];

if(source == TicTac[0])
TicTac[0].setText(letter4User);

else

if(source == TicTac[1])
TicTac[1].setText(letter4User);

else

if(source == TicTac[2])
TicTac[2].setText(letter4User);

else

if(source == TicTac[3])
TicTac[3].setText(letter4User);

else

if(source == TicTac[4])
TicTac[4].setText(letter4User);

else

if(source == TicTac[5])
TicTac[5].setText(letter4User);

else

if(source == TicTac[6])
TicTac[6].setText(letter4User);

else

if(source == TicTac[7])
TicTac[7].setText(letter4User);

else

if(source == TicTac[8])
TicTac[8].setText(letter4User);

else


if(random == 1 )
TicTac[0].setText(letterForPC);


do
r=random.nextInt(9);
while (position[r]!=BLANK);


Random seed = new Random();
int n = seed.nextInt(3);



{



}


}






}


Mod edit - Please code.gif
~BetaWar
User is offlineProfile CardPM
+Quote Post

BetaWar
RE: Tic Tac Toe
15 Oct, 2008 - 08:02 AM
Post #2

#include <soul.h>
Group Icon

Joined: 7 Sep, 2006
Posts: 2,022



Thanked: 81 times
Dream Kudos: 1175
My Contributions
All you need for the computer move is a function that picks a random number, and then checks to see if that index in the array is filled with an x or o, if neither the computer can move there.

java
int random(int min, int max){ //called with random(<MIN>, <MAX+1>)
java.util.Random rand = new java.util.Random();
return rand.nextInt((max-min))+min;
}


Then, you have it loop through the array and see if the value at index <returned_random_number> is " ", if it is you can take that space.

Hope that helps.
User is offlineProfile CardPM
+Quote Post

BradburyRob
RE: Tic Tac Toe
15 Oct, 2008 - 05:32 PM
Post #3

New D.I.C Head
*

Joined: 14 Oct, 2008
Posts: 5


My Contributions
I think I almost have a TicTacToe game working, in the actionPerformed method I included a random number so the computer would make a move on the grid right after a user make a move. Well, this is where I am stucked! I make a move, then the PC makes a move, so far so good but after I make a second move the applet does not make another move.

I double checked the loop and cannot see anything wrong, the way a read it, It should make a move after I do each time.

I am clearly missing something and I just dont know what.

If anyone steps in THXs in advance.


CODE
import java.util.*;
import java.util.Random;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JTicTacToe extends JApplet implements ActionListener
{



    JLabel someText = new JLabel(" Enter the number of expected guest for the event:  ");

    JTextField firstNum = new JTextField(15);

    JLabel test_two = new JLabel("");

    Container test = getContentPane();

    Random rand = new java.util.Random();

    String letter4User = "X";

    String letterForPC = "O";

    String[] dummyInput = {" ", " ", " ", " ", " ", " ", " ", " ", " "};
  
    int   [] verify     = {   0,   1,  2,   3,   4,   5,   6,   7,   8};

        JButton[] TicTac = new JButton[dummyInput.length];

    int random = ((int) (Math.random() * 100) % 9 + 1);
    
    Font convertion = new Font ("Bookman Old Style", Font.ITALIC, 13);



    public void init()

    {
        
      test.setLayout(new FlowLayout());

      firstNum.addActionListener(this);
            
      for(int y = 0; y < dummyInput.length; y++)
           {
          TicTac[y] = new JButton(dummyInput[y]);
    
      test.add(TicTac[y]);

      TicTac[y].addActionListener(this);        
        
           }

    }
    
    public void actionPerformed(ActionEvent r)


    {

      
     Object source = r.getSource();

    
    for(int b = 0; b < TicTac.length; b++)
    if(source == TicTac[b] )
        {
        TicTac[b].setText(letter4User);

        TicTac[random].setText(letterForPC);    

        }    
    
    }


}




User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 12:07PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month