We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Tic-tac-toe problem
Page Index Toggle Pages: 1
Tic-tac-toe problem (Read 1565 times)
Tic-tac-toe problem
Feb 5th, 2010, 3:46pm
 
Hello,
I must write a tic-tac-toe game on processing.
This is a code of my program
Code:

void setup()
{size (300,300);
}

void draw()
{
background (255,0,0);

line (100,0,100,300);
line (200,0,200,300);
line (0,100,300,100);
line (0,200,300,200);

}

int z=0;
void mousePressed()

{
z++;

if ((mouseX>0)&&(mouseX<100)&&(mouseY>0)&&(mouseY<100))

if (z==0)
{
strokeWeight(20);
point(mouseX,mouseY);
}
else if (z==1)
{
strokeWeight(10);
rect(mouseX,mouseY,20,20);
}




if ((mouseX>100)&&(mouseX<200)&&(mouseY>0)&&(mouseY<100))

if (z%2==1)
{
strokeWeight(20);
point(mouseX,mouseY);
}
else
{
strokeWeight(10);
point(mouseX,mouseY);
}



if ((mouseX>200)&&(mouseX<300)&&(mouseY>0)&&(mouseY<100))

if (z%2==1)
{
strokeWeight(20);
point(mouseX,mouseY);
}
else
{
strokeWeight(10);
point(mouseX,mouseY);
}



if ((mouseX>0)&&(mouseX<100)&&(mouseY>100)&&(mouseY<200))

if (z%2==1)
{
strokeWeight(20);
point(mouseX,mouseY);
}
else
{
strokeWeight(10);
point(mouseX,mouseY);
}



if ((mouseX>100)&&(mouseX<200)&&(mouseY>100)&&(mouseY<200))

if (z%2==1)
{
strokeWeight(20);
point(mouseX,mouseY);
}
else
{
strokeWeight(10);
point(mouseX,mouseY);
}



if ((mouseX>200)&&(mouseX<300)&&(mouseY>100)&&(mouseY<200))

if (z%2==1)
{
strokeWeight(20);
point(mouseX,mouseY);
}
else
{
strokeWeight(10);
point(mouseX,mouseY);
}




if ((mouseX>0)&&(mouseX<100)&&(mouseY>200)&&(mouseY<300))

if (z%2==1)
{
strokeWeight(20);
point(mouseX,mouseY);
}
else
{
strokeWeight(10);
point(mouseX,mouseY);
}



if ((mouseX>100)&&(mouseX<200)&&(mouseY>200)&&(mouseY<300))

if (z%2==1)
{
strokeWeight(20);
point(mouseX,mouseY);
}
else
{
strokeWeight(10);
point(mouseX,mouseY);
}



if ((mouseX>200)&&(mouseX<300)&&(mouseY>200)&&(mouseY<300))

if (z%2==1)
{
strokeWeight(20);
point(mouseX,mouseY);
}
else
{
strokeWeight(10);
point(mouseX,mouseY);
}



}


And I don't understand why this is not working... Could someone help me ? It will be the best when someone could write a simply Tic-tac-toe game on processing. This game might be for 2 players not player vs computer. I will be grateful for that ;]
Re: Tic-tac-toe problem
Reply #1 - Feb 5th, 2010, 8:39pm
 
You need to have your X and O draw in the draw().  Your not seeing anything because it is instantly overwritten.  So make your mousepressed enable something that can be placed in the draw() so that it is redrawn each frame.

Find the area that it's in during the mousepress... then perhaps add some boolean triggers.  Then use a couple lines and a ellipse to represent the X and O.

Something like...
Code:

boolean quad1, quad1val, value;
void draw() {
if (quad1 == true) {
  if (quad1val == true) {
//line (...
//line (...
  } else {
//ellipse
  }
}
}

void mousePressed() {
value = !value;
 if ((mouseX>200)&&(mouseX<300)&&(mouseY>200)&&(mouseY<300)) {
   quad1 = true;
   quad1val = value;
  }
}
Re: Tic Tac Toe problem ;/ Help me please ;]
Reply #2 - Feb 6th, 2010, 12:59am
 
Quote:
// This array is used to keep track of where players have gone.
int[] grid;
// This boolean is used to keep track of who's turn it is.
boolean turn;

void setup(){
  size(300,300);
  noCursor();
  // Set up initial values;
  grid = new int[9]; // defaults to {0, 0, 0, ... }
  turn = false;
}

void draw(){
  // For each grid space
  for( int i=0;i<3;i++){
    for( int j=0;j<3;j++){
      // Determine what color needs to go there.
      switch( grid[3*i+j] ){
      case 1:
        fill(255,0,0);
        break;
      case -1:
        fill(0,0,255);
        break;
      case 0:
      default:
        fill(0);
        break;
      }
      // And draw a square.
      rect(width/3.0*i, height/3.0*j, width/3.0, height/3.0);
    }
  } 
  // Also draw a colored square for a mouse cursor, so you know who's go it is.
  fill(turn?color(128,0,0):color(0,0,128));
  rect(mouseX-5, mouseY-5, 10, 10);
}

void mouseClicked(){
  int jClicked = 0;
  int iClicked = 0;
  // Reset with the right mouse button.
  if( mouseButton == RIGHT ){
    for( int i=0; i<9; i++){
      grid[i] = 0;
    }
    return;
  }
  // Otherwise try to place a piece
  // Work out where the mouse was clicked.
  if(mouseX>width/3.0){
    jClicked++;   
  }
  if(mouseX>2*width/3.0){
    jClicked++;   
  }
  if(mouseY>height/3.0){
    iClicked++;   
  }
  if(mouseY>2*height/3.0){
    iClicked++;   
  }
  // If this spot is blank, fill it with the right color...
  if( grid[jClicked*3+iClicked] == 0 ){
    grid[jClicked*3+iClicked] = turn?1:-1;
    // Check for a win...
    //checkForWin(); // Not implemented.
    // And make it the other player's turn.
    turn = !turn;
    //If that is a computer, do a move for them.
    //if( turn ){doComputerMove();} // Not implemented.
  }
}



This still needs a lot of work, as it's just a basic framework.
Re: Tic-tac-toe problem
Reply #3 - Feb 6th, 2010, 2:21am
 
The last 1 Posts were moved here from Syntax Questions by PhiLho.
[Moderator's note: In clear, TfGuy44's answer have been moved from a duplicate thread. Kozatrix22, don't post twice the same message in different sections, please.]
Page Index Toggle Pages: 1