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
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
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, "!" FinSiFinAlgoritmoFlowchart
Each block in the flowchart matches one instruction in the code.
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.
Related examples
Shell Sort
Insertion sort between far-apart elements, with a gap that shrinks every pass.
AdvancedQuicksort
Picks a pivot, splits smaller from larger values and sorts each side recursively.
AdvancedHeap Sort
Arranges the array as a heap, then takes the largest out again and again.
Advanced