Menu
CoddyTech

Recap - Game Board

Part of the Logic & Flow section of Coddy's GDScript journey. Lesson 18 of 78.

challenge icon

Challenge

Easy

Check a game of tic-tac-toe. The program reads three lines, the rows of the board from top to bottom. Each line has three characters: X, O, or . for an empty square.

  1. Store the board in board, a 2D array of 3 rows, where each cell is a one-letter string.
  2. A player wins with three of their letter in one row, one column or one of the two diagonals.
  3. Print one line: X wins or O wins; if nobody won and the board has no . left, Draw; otherwise Keep playing.

Every board in the tests has at most one winner. For the input below, the output is O wins, because the middle column holds three O:

XO.
XO.
.OX

Try it yourself

extends Node

func _ready():
	var board := []

	# Read the three rows into board

	# Check the rows, the columns and the diagonals, then print the result
	pass

All lessons in Logic & Flow

Practice on your own: Online GDScript compiler