Skip to content
ExamplesEspañolWorkspace →

Tic-Tac-Toe

The classic three-in-a-row game for two players.

Level
Advanced
Topic:
Projects
Concepts:
Input & output
Matrices
SubProcesos
Functions
Repetir
Mientras
Logic
Open in Codoritmo

Run, change and debug this example in your browser.

What you'll learn

The board is a 3 x 3 matrix. A SubProceso draws it, a Logico Funcion checks for three in a row, and Repetir ... Hasta Que validates each move.

Code

tic-tac-toe.psc · 79 lines
SubProceso DibujarTablero(tablero)    Definir fila Como Entero    Escribir ""    Para fila <- 1 Hasta 3 Hacer        Escribir " ", tablero[fila, 1], " | ", tablero[fila, 2], " | ", tablero[fila, 3]        Si fila < 3 Entonces            Escribir "---+---+---"        FinSi    FinParaFinSubProcesoFuncion gano <- HayTresEnLinea(tablero, jugador)    Definir gano Como Logico    Definir i Como Entero    gano <- Falso    Para i <- 1 Hasta 3 Hacer        Si tablero[i, 1] = jugador Y tablero[i, 2] = jugador Y tablero[i, 3] = jugador Entonces            gano <- Verdadero        FinSi        Si tablero[1, i] = jugador Y tablero[2, i] = jugador Y tablero[3, i] = jugador Entonces            gano <- Verdadero        FinSi    FinPara    Si tablero[1, 1] = jugador Y tablero[2, 2] = jugador Y tablero[3, 3] = jugador Entonces        gano <- Verdadero    FinSi    Si tablero[1, 3] = jugador Y tablero[2, 2] = jugador Y tablero[3, 1] = jugador Entonces        gano <- Verdadero    FinSiFinFuncionAlgoritmo TaTeTi    Definir tablero, jugador, ganador Como Caracter    Definir fila, columna, jugadas, i, j Como Entero    Definir valida Como Logico    Dimension tablero[3, 3]    Para i <- 1 Hasta 3 Hacer        Para j <- 1 Hasta 3 Hacer            tablero[i, j] <- " "        FinPara    FinPara    jugador <- "X"    ganador <- ""    jugadas <- 0    Mientras ganador = "" Y jugadas < 9 Hacer        DibujarTablero(tablero)        Repetir            Escribir "Turno de ", jugador, ". Fila (1 a 3):"            Leer fila            Escribir "Columna (1 a 3):"            Leer columna            valida <- fila >= 1 Y fila <= 3 Y columna >= 1 Y columna <= 3            Si valida Entonces                Si tablero[fila, columna] <> " " Entonces                    valida <- Falso                FinSi            FinSi            Si NO valida Entonces                Escribir "Esa casilla no está disponible."            FinSi        Hasta Que valida        tablero[fila, columna] <- jugador        jugadas <- jugadas + 1        Si HayTresEnLinea(tablero, jugador) Entonces            ganador <- jugador        FinSi        Si jugador = "X" Entonces            jugador <- "O"        SiNo            jugador <- "X"        FinSi    FinMientras    DibujarTablero(tablero)    Si ganador = "" Entonces        Escribir "¡Empate!"    SiNo        Escribir "¡Ganó ", ganador, "!"    FinSiFinAlgoritmo

Flowchart

Each block in the flowchart matches one instruction in the code.

Algoritmo TaTeTi
Algoritmo TaTeTiSíNoSíNoSíNoNoSíSíNoSíNoSíNoSíNoAlgoritmo TaTeTiAlgoritmo TaTeTiDefinir tablero, jugador, ganador Como CaracterDefinir tablero, jugador,ganador Como CaracterDefinir fila, columna, jugadas, i, j Como EnteroDefinir fila, columna,jugadas, i, j Como EnteroDefinir valida Como LogicoDefinir valida Como LogicoDimension tablero[3, 3]Dimension tablero[3, 3]Para i <- 1 Hasta 3 HacerPara i <- 1 Hasta 3Para j <- 1 Hasta 3 HacerPara j <- 1 Hasta 3tablero[i, j] <- " "tablero[i, j] <- " "jugador <- "X"jugador <- "X"ganador <- ""ganador <- ""jugadas <- 0jugadas <- 0Mientras ganador = "" Y jugadas < 9 Hacerganador = "" Yjugadas < 9DibujarTablero(tablero)DibujarTablero(tablero)Escribir "Turno de ", jugador, ". Fila (1 a 3):"Escribir "Turno de ", jugador,". Fila (1 a 3):"Leer filaLeer filaEscribir "Columna (1 a 3):"Escribir "Columna (1 a 3):"Leer columnaLeer columnavalida <- fila >= 1 Y fila <= 3 Y columna >= 1 Y columna <= 3valida <- fila >= 1 Y fila <=3 Y columna >= 1 Y columna <=3Si valida EntoncesvalidaSi tablero[fila, columna] <> " " Entoncestablero[fila,columna] <> " "valida <- Falsovalida <- FalsoSi NO valida EntoncesNO validaEscribir "Esa casilla no está disponible."Escribir "Esa casilla no estádisponible."Hasta Que validavalidatablero[fila, columna] <- jugadortablero[fila, columna] <-jugadorjugadas <- jugadas + 1jugadas <- jugadas + 1Si HayTresEnLinea(tablero, jugador) EntoncesHayTresEnLinea(tablero, jugador)ganador <- jugadorganador <- jugadorSi jugador = "X" Entoncesjugador = "X"jugador <- "O"jugador <- "O"jugador <- "X"jugador <- "X"DibujarTablero(tablero)DibujarTablero(tablero)Si ganador = "" Entoncesganador = ""Escribir "¡Empate!"Escribir "¡Empate!"Escribir "¡Ganó ", ganador, "!"Escribir "¡Ganó ", ganador,"!"FinAlgoritmoFinAlgoritmo
SubProceso DibujarTablero
SubProceso DibujarTableroSíNoSubProceso DibujarTablero(tablero)SubProceso DibujarTablero(tablero)Definir fila Como EnteroDefinir fila Como EnteroEscribir ""Escribir ""Para fila <- 1 Hasta 3 HacerPara fila <- 1 Hasta 3Escribir " ", tablero[fila, 1], " | ", tablero[fila, 2], " | ", tablero[fila, 3]Escribir " ", tablero[fila,1], " | ", tablero[fila, 2], "| ", tablero[fila, 3]Si fila < 3 Entoncesfila < 3Escribir "---+---+---"Escribir "---+---+---"FinSubProcesoFinSubProceso
Funcion HayTresEnLinea
Funcion HayTresEnLineaSíNoSíNoSíNoSíNoFuncion gano <- HayTresEnLinea(tablero, jugador)Funcion gano <-HayTresEnLinea(tablero, jugador)Definir gano Como LogicoDefinir gano Como LogicoDefinir i Como EnteroDefinir i Como Enterogano <- Falsogano <- FalsoPara i <- 1 Hasta 3 HacerPara i <- 1 Hasta 3Si tablero[i, 1] = jugador Y tablero[i, 2] = jugador Y tablero[i, 3] = jugador Entoncestablero[i, 1] =jugador Y tablero[i,2] = jugador Y…gano <- Verdaderogano <- VerdaderoSi tablero[1, i] = jugador Y tablero[2, i] = jugador Y tablero[3, i] = jugador Entoncestablero[1, i] =jugador Y tablero[2,i] = jugador Y…gano <- Verdaderogano <- VerdaderoSi tablero[1, 1] = jugador Y tablero[2, 2] = jugador Y tablero[3, 3] = jugador Entoncestablero[1, 1] =jugador Y tablero[2,2] = jugador Y…gano <- Verdaderogano <- VerdaderoSi tablero[1, 3] = jugador Y tablero[2, 2] = jugador Y tablero[3, 1] = jugador Entoncestablero[1, 3] =jugador Y tablero[2,2] = jugador Y…gano <- Verdaderogano <- VerdaderoFinFuncion (devuelve gano)FinFuncion (devuelve gano)

Sample run

This is what the program shows when it runs. The answers typed by the user are highlighted.

    |   |  ---+---+---   |   |  ---+---+---   |   |  Turno de X. Fila (1 a 3):User answer: 1Columna (1 a 3):User answer: 1  X |   |  ---+---+---   |   |  ---+---+---   |   |  Turno de O. Fila (1 a 3):User answer: 2Columna (1 a 3):User answer: 1  X |   |  ---+---+--- O |   |  ---+---+---   |   |  Turno de X. Fila (1 a 3):User answer: 1Columna (1 a 3):User answer: 2  X | X |  ---+---+--- O |   |  ---+---+---   |   |  Turno de O. Fila (1 a 3):User answer: 2Columna (1 a 3):User answer: 2  X | X |  ---+---+--- O | O |  ---+---+---   |   |  Turno de X. Fila (1 a 3):User answer: 1Columna (1 a 3):User answer: 3  X | X | X---+---+--- O | O |  ---+---+---   |   |  ¡Ganó X!

Challenge

When the game ends, ask whether they want to play again.

Show hint

Wrap the whole game, starting from clearing the board, in a Repetir ... Hasta Que.

Open in Codoritmo
  • Shell Sort

    Insertion sort between far-apart elements, with a gap that shrinks every pass.

    Advanced
  • Quicksort

    Picks a pivot, splits smaller from larger values and sorts each side recursively.

    Advanced
  • Heap Sort

    Arranges the array as a heap, then takes the largest out again and again.

    Advanced