Tic-Tac-Toe Game

Game development specific discussions.
Post Reply
syn9
Posts: 186
Joined: May 27, 2005 17:11
Contact:

Tic-Tac-Toe Game

Post by syn9 »

Here's a little tic-tac-toe game. Thanks to ImortisInglorian on the discord for some feedback on my rock-paper-scissors game, I was able to include some of that feedback here.

Code below as well as here: https://github.com/Syn-Nine/freebasic-mini-games

Code: Select all

''-----------------------------------------------------------------------------
'' Tic-Tac-Toe game by Syn9
''-----------------------------------------------------------------------------

''-----------------------------------------------------------------------------
'' Utility Functions
''-----------------------------------------------------------------------------
namespace util

function console_input() as string
	dim as string temp
	input temp
	return temp
end function


''-----------------------------------------------------------------------------
function rand_range(lhs as integer, rhs as integer) as integer
	dim as integer ret = lhs + int(rnd * (rhs - lhs)) ' [lhs, rhs)
	return ret
end function


''-----------------------------------------------------------------------------
declare function to_int overload (value as string) as integer

function to_int(value as string) as integer
	return valint(value)
end function


end namespace
''-----------------------------------------------------------------------------


randomize

''-----------------------------------------------------------------------------
'' Global Enumeration
''-----------------------------------------------------------------------------

enum enumtype
    ENUM_COMPUTER
    ENUM_EMPTY
    ENUM_PLAYER
end enum


''-----------------------------------------------------------------------------
'' Function Declarations
''-----------------------------------------------------------------------------

declare function check_win(who as enumtype) as boolean
declare sub draw_board()
declare sub draw_cell(idx as long)
declare sub get_player_input()
declare sub get_computer_input()

''-----------------------------------------------------------------------------
'' Entry Point
''-----------------------------------------------------------------------------

dim shared as long SZ = 9
dim shared as enumtype board(9)
for i as integer = 0 to 9 - 1
    board(i) = ENUM_EMPTY
next

print "Let's play Tic-Tac-Toe!"
print "-----------------------------------------------------"

while true
    draw_board()
    get_player_input()
    if check_win(ENUM_PLAYER) then
        exit while
    end if
    get_computer_input()
    if check_win(ENUM_COMPUTER) then
        exit while
    end if
wend
draw_board()

''-----------------------------------------------------------------------------
'' Function Definitions
''-----------------------------------------------------------------------------

function check_win(who as enumtype) as boolean
    dim as boolean ret = false
    dim as long idx = 0
	
    for row as integer = 0 to 3 - 1
        idx = row * 3
        if who = board(idx) AndAlso who = board(idx + 1) AndAlso who = board(idx + 2) then
            ret = true
        end if
    next
	
    for col as integer = 0 to 3 - 1
        idx = col
        if who = board(idx) AndAlso who = board(idx + 3) AndAlso who = board(idx + 6) then
            ret = true
        end if
    next
	
    if who = board(4) then
        if who = board(0) AndAlso who = board(8) then
            ret = true
        elseif who = board(2) AndAlso who = board(6) then
            ret = true
        end if
    end if
	
    if ret then
        if ENUM_PLAYER = who then
            print "Player Wins!"
        else
            print "Computer Wins!"
        end if
    else
        for i as integer = 0 to SZ - 1
            if ENUM_EMPTY = board(i) then
                exit for
            end if
            if 8 = i then
                ret = true
                print "DRAW!"
            end if
        next
    end if
    return ret
end function

sub draw_board()
    print "/-----------\ "
    print "| ";
    draw_cell(0)
    print " | ";
    draw_cell(1)
    print " | ";
    draw_cell(2)
    print " | "
    print "|---|---|---|"
    print "| ";
    draw_cell(3)
    print " | ";
    draw_cell(4)
    print " | ";
    draw_cell(5)
    print " | "
    print "|---|---|---|"
    print "| ";
    draw_cell(6)
    print " | ";
    draw_cell(7)
    print " | ";
    draw_cell(8)
    print " | "
    print "\-----------/"
end sub

sub draw_cell(idx as long)
    if ENUM_EMPTY = board(idx) then
        print str(( idx + 1 ));
    elseif ENUM_PLAYER = board(idx) then
        print "X";
    elseif ENUM_COMPUTER = board(idx) then
        print "O";
    end if
end sub

sub get_player_input()
    while true
        print "Enter the # for your choice (X)"
        dim as long temp = util.to_int(util.console_input())
        if temp > 0 AndAlso temp < 10 then
            if ENUM_EMPTY = board(temp - 1) then
                board(temp - 1) = ENUM_PLAYER
                exit while
            end if
        end if
        print "Invalid choice."
    wend
end sub

sub get_computer_input()
    while true
        dim as long choice = util.rand_range(0, SZ)
        if ENUM_EMPTY = board(choice) then
            board(choice) = ENUM_COMPUTER
            exit while
        end if
    wend
end sub
Last edited by syn9 on Jun 06, 2025 1:26, edited 2 times in total.
Lothar Schirm
Posts: 492
Joined: Sep 28, 2013 15:08
Location: Germany

Re: Tic-Tac-Toe Game

Post by Lothar Schirm »

I like your nice and simple games! :)
dodicat
Posts: 8267
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Tic-Tac-Toe Game

Post by dodicat »

syn9 wrote: May 27, 2025 5:13 Here's a little tic-tac-toe game. Thanks to ImortisInglorian on the discord for some feedback on my rock-paper-scissors game, I was able to include some of that feedback here.

It is maybe deliberate for you, but your function rand_range() does not hit the rhs limit.
Here is a comparison showing hits:

Code: Select all

#cmdline "-exx"
function rand_range(lhs as integer, rhs as integer) as integer
	dim as integer ret = lhs + int(rnd * (rhs - lhs))
	return ret
end function

function range(lhs as integer, rhs as integer) as integer
    return Int(Rnd*((rhs+1)-(lhs))+(lhs))
end function


dim as long a(1 to 5)
for n as long=1 to 1000000
    a(rand_range(1,5))+=1
next

for n as long=lbound(a) to ubound(a)
    print n,a(n)
    a(n)=0
next
print
 
 for n as long=1 to 1000000
    a(range(1,5))+=1
next

for n as long=lbound(a) to ubound(a)
    print n,a(n)
next
 
sleep 
syn9
Posts: 186
Joined: May 27, 2005 17:11
Contact:

Re: Tic-Tac-Toe Game

Post by syn9 »

yup, that's deliberate. I added a little comment in the function to make it a bit more clear. I might need to make it more obvious but not sure how to word it...

thanks for mentioning, that could trip people up.
dodicat
Posts: 8267
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Tic-Tac-Toe Game

Post by dodicat »

Thanks syn9.
Commented like a maths person.
Nice neat game.
Post Reply