day03_oxo/sketch.js
//The function draw is called every frame:
The function draw is called every frame.
The function draw is called every frame .
function draw(){
  if(gameState==="play_game")
  {
    var c = color(255,0,0);
    for(var i = 0; i < pieces.length; i++)
    {
      pieces[i].display(c);
    }
    for(i = 0; i <winStates.length; i++)
    {
      if(pieces[winStates[i][0]].name==="X" && pieces[winStates[i][1]].name==="X" && pieces[winStates[i][2]].name==="X")
      {
        winner = "X";
        gameState = "end_game";
      } else if(pieces[winStates[i][0]].name==="O" &&  pieces[winStates[i][1]].name==="O" && pieces[winStates[i][2]].name==="O")
        {
          winner = "O";
          gameState = "end_game";
        }
      }
  } else if(gameState==="end_game")
    {
      background(0);
      fill(255);
      textSize(24);
      text( winner + " is the winner", width/2,height/2);
    }
}
function draw ( ) { if ( gameState === "play_game" ) { var c = color ( 255 , 0 , 0 ) ; for ( var i = 0 ; i < pieces . length ; i ++ ) { pieces [ i ] . display ( c ) ; } for ( i = 0 ; i < winStates . length ; i ++ ) { if ( pieces [ winStates [ i ] [ 0 ] ] . name === "X" && pieces [ winStates [ i ] [ 1 ] ] . name === "X" && pieces [ winStates [ i ] [ 2 ] ] . name === "X" ) { winner = "X" ; gameState = "end_game" ; } else if ( pieces [ winStates [ i ] [ 0 ] ] . name === "O" && pieces [ winStates [ i ] [ 1 ] ] . name === "O" && pieces [ winStates [ i ] [ 2 ] ] . name === "O" ) { winner = "O" ; gameState = "end_game" ; } } } else if ( gameState === "end_game" ) { background ( 0 ) ; fill ( 255 ) ; textSize ( 24 ) ; text ( winner + " is the winner" , width / 2 , height / 2 ) ; } }
The function draw is called every frame:
The function draw is called every frame :
//If the current game state is "play_game"... //Set the variable "c" to a red colour: //Loop this for as many times as there are GamePiece objects (the length of the pieces array): //Call the display() funtion of each GamePiece object in the pieces array, which draws the tile on the canvas: //This loop iterates through the possible win states for the game and checks if someone has won: //If the tiles of the current win state all have the name "x" then the winner is "x" and the game ends: //If not, but the same tiles all have the name "o", then the winner is "o" and the game ends: //If the current state is not "play_game", but "end_game"... //Make the background black, set the fill colour to white and display the winner of the game at the middle of the canvas: