
Task

Play a game of tic-tac-toe.

Ensure that legal moves are played and that a winning position is notified.


Tic-tac-toe   is also known as:

   naughts and crosses 
   tic tac toe 
   tick tack toe 
   three in a row 
   tres en rayo       and
   Xs  and  Os


See also
   MathWorld™, Tic-Tac-Toe game.
   Wikipedia tic-tac-toe.


 
# basado en código de Antonio Rodrigo dos Santos Silva (gracias):
# http://statusgear.freeforums.net/thread/17/basic-256-tic-tac-toe
 
global playerturn$
global endGame$
global space$
global player1Score$
global player2Score$
global invalidMove$
global tecla$
global keyQ$
global keyW$
global keyE$
global keyA$
global keyS$
global keyD$
global keyZ$
global keyX$
global keyC$
global keySpace$
global keyEsc$
 
keyQ$ = 81
keyW$ = 87
keyE$ = 69
keyA$ = 65
keyS$ = 83
keyD$ = 68
keyZ$ = 90
keyX$ = 88
keyC$ = 67
keySpace$ = 32
keyEsc$ = 16777216
 
dim space$(9)
 
 
subroutine clearGameVars()
  playerturn$ = 1
  invalidMove$ = 0
  endGame$ = 0
  tecla$ = 0
 
  for t = 0 to space$[?]-1
    space$[t] = 0
  next t
end subroutine
 
subroutine endGame()
  cls
  print "¡Hasta pronto!..."
  end
end subroutine
 
subroutine printBoard()
  print "          " + space$[0]+" | "+space$[1]+" | "+space$[2]
  print "          " + "— + — + —"
  print "          " + space$[3]+" | "+space$[4]+" | "+space$[5]
  print "          " + "— + — + —"
  print "          " + space$[6]+" | "+space$[7]+" | "+space$[8]
  print ""
end subroutine
 
subroutine changePlayer()
  if playerturn$ = 1 then
    playerturn$ = 2
  else
    playerturn$ = 1
  end if
end subroutine
 
subroutine endMatchWithWinner()
  cls
  call printPlayerScore()
  call printBoard()
  endGame$ = 1
 
  if playerturn$ = 1 then
    player1Score$ += 1
  else
    player2Score$ += 1
  end if
 
  print "¡Jugador " + playerturn$ + " gana!" + chr(10)
  print "Pulsa [SPACE] para jugar otra partida"
  print "Pulsa [ESC] para dejar de jugar"
  do
    tecla$ = key
    pause .01
    if tecla$ = keySpace$ then call gamePlay()
    if tecla$ = keyEsc$ then call endGame()
  until false
end subroutine
 
subroutine endMatchWithoutWinner()
  cls
  call printPlayerScore()
  call printBoard()
  endGame$ = 1
 
  print "                 Nadie ganó :(                  " + chr(10)
  print " Pulsa [SPACE] para comenzar o [ESC] para salir. "
  do
    tecla$ = key
    pause .01
    if tecla$ = keySpace$ then call gamePlay()
    if tecla$ = keyEsc$ then call endGame()
  until false
end subroutine
 
subroutine printPlayerScore()
  print "--------------------------------------------"
  print " Jugador #1: " + player1Score$ + " pts"
  print " Jugador #2: " + player2Score$ + " pts"
  print "--------------------------------------------" + chr(10)
end subroutine
 
 
subroutine printPlayerMessage()
  print "Jugador: " + playerturn$ + ", elige una casilla, por favor."
end subroutine
 
subroutine gamePlay()
  call clearGameVars()
  cls
  call printPlayerScore()
  call printBoard()
  call printPlayerMessage()
  while 0 = 0
    invalidMove$ = 0
 
    if endGame$ = 0 then
      do
        tecla$ = key
        pause .01
        validKeypressed$ = 0
        if tecla$ = keyQ$ or tecla$ = keyW$  or tecla$ = keyE$ or tecla$ = keyA$ or tecla$ = keyS$ or tecla$ = keyD$ or tecla$ = keyZ$ or tecla$ = keyX$ or tecla$ = keyC$ then validKeypressed$ = 1
      until validKeypressed$ = 1
    endif
 
    if tecla$ = keyQ$ then
      if space$[0] = 0 then
        space$[0] = playerturn$
      else
        invalidMove$ = 1
      endif
    endif
 
    if tecla$ = keyW$ then
      if space$[1] = 0 then
        space$[1] = playerturn$
      else
        invalidMove$ = 1
      endif
    endif
 
    if tecla$ = keyE$ then
      if space$[2] = 0 then
        space$[2] = playerturn$
      else
        invalidMove$ = 1
      endif
    endif
 
    if tecla$ = keyA$ then
      if space$[3] = 0 then
        space$[3] = playerturn$
      else
        invalidMove$ = 1
      endif
    endif
 
    if tecla$ = keyS$ then
      if space$[4] = 0 then
        space$[4] = playerturn$
      else
        invalidMove$ = 1
      endif
    endif
 
    if tecla$ = keyD$ then
      if space$[5] = 0 then
        space$[5] = playerturn$
      else
        invalidMove$ = 1
      endif
    endif
 
    if tecla$ = keyZ$ then
      if space$[6] = 0 then
        space$[6] = playerturn$
      else
        invalidMove$ = 1
      endif
    endif
 
    if tecla$ = keyX$ then
      if space$[7] = 0 then
        space$[7] = playerturn$
      else
        invalidMove$ = 1
      endif
    endif
 
    if tecla$ = keyC$ then
      if space$[8] = 0 then
        space$[8] = playerturn$
      else
        invalidMove$ = 1
      endif
    endif
 
    if invalidMove$ = 0 then
      tecla$ = 0
 
      if space$[0] = 1 and space$[1] = 1 and space$[2] = 1 then call endMatchWithWinner()
      if space$[3] = 1 and space$[4] = 1 and space$[5] = 1 then call endMatchWithWinner()
      if space$[6] = 1 and space$[7] = 1 and space$[8] = 1 then call endMatchWithWinner()
      if space$[0] = 1 and space$[3] = 1 and space$[6] = 1 then call endMatchWithWinner()
      if space$[1] = 1 and space$[4] = 1 and space$[7] = 1 then call endMatchWithWinner()
      if space$[2] = 1 and space$[5] = 1 and space$[8] = 1 then call endMatchWithWinner()
      if space$[0] = 1 and space$[4] = 1 and space$[8] = 1 then call endMatchWithWinner()
      if space$[2] = 1 and space$[4] = 1 and space$[6] = 1 then call endMatchWithWinner()
      if space$[0] = 2 and space$[1] = 2 and space$[2] = 2 then call endMatchWithWinner()
      if space$[3] = 2 and space$[4] = 2 and space$[5] = 2 then call endMatchWithWinner()
      if space$[6] = 2 and space$[7] = 2 and space$[8] = 2 then call endMatchWithWinner()
      if space$[0] = 2 and space$[3] = 2 and space$[6] = 2 then call endMatchWithWinner()
      if space$[1] = 2 and space$[4] = 2 and space$[7] = 2 then call endMatchWithWinner()
      if space$[2] = 2 and space$[5] = 2 and space$[8] = 2 then call endMatchWithWinner()
      if space$[0] = 2 and space$[4] = 2 and space$[8] = 2 then call endMatchWithWinner()
      if space$[2] = 2 and space$[4] = 2 and space$[6] = 2 then call endMatchWithWinner()
 
      if space$[0] <> 0 and space$[1] <> 0 and space$[2] <> 0 and space$[3] <> 0 and space$[4] <> 0 and space$[5] <> 0 and space$[6] <> 0 and space$[7] <> 0 and space$[8] <> 0 then call endMatchWithoutWinner()
 
      call changePlayer()
      cls
      call printPlayerScore()
      call printBoard()
      call printPlayerMessage()
    end if
 
  end while
end subroutine
 
subroutine gameMenu()
  cls
  call clearGameVars()
 
  player1Score$ = 0
  player2Score$ = 0
 
  print "================================================="
  print "|                  TIC-TAC-TOE                  |"
  print "=================================================" + chr(10)
  print "  Teclas para jugar:"
  print "---------------------"
  print "    | q | w | e |"
  print "    | a | s | d |"
  print "    | z | x | c |" + chr(10)
  print " Pulsa [SPACE] para comenzar o [ESC] para salir. "
 
  do
    tecla$ = key
    pause .01
    if tecla$ = keySpace$ then call gamePlay()
    if tecla$ = keyEsc$ then call endGame()
  until false
end subroutine
 
call gameMenu()
end
 