Posted: Fri Mar 09, 2012 9:10 am Post subject: Tic-Tac-Toe
So here's a basic tic-tac-toe game, with a bot included...
Turing:
/* Tic-Tac-Toe game
* By: A. Prado
* AI Included
* AI Instructions: Turn the variable named "Steve" off to play a two-player game
* AI Code breakdown:
* The AI, herein named "Steve", works by a combination of
* random cell location and intelligent processing. It will,
* at first, check all rows/columns/diagonals to see if they are full,
* then it will check the ones that arent full for possibilities of
* blocking the player, or winning. If it can do neither, it will
* randomize a location, check if it's available, and place the O
* in the cell if it is, repeat the process if it is not.
*/
View.Set("graphics:500;600") var x, y, b :int% X and Y coordinates, button variables var winsX, winsO, winNo :int:=0% Counter for wins, losses, and ties var font, fontWins :int var turn :boolean:=false% True for X, false for O var picX, picO :int% Picture variable for the X and O var randCell :int% Random cell variable for AI var steve :boolean:=true% Set Steve to "False" to play 2 player game var turns :int:=1% Variable counting who's turn it is var gameOn, exitVar :boolean:=false% Flags for exiting the whole game loop, or single game instance var keyCheck :arraycharofboolean% Array controlling keyboard input var AICheck :array1.. 8ofboolean:= init(false, false, false, false, false, false, false, false)% Row/Column/Diagonal check array var posX, posO :array1.. 8ofint:= init(0, 0, 0, 0, 0, 0, 0, 0)% Row/Column/Diagonal points counter (O and X) var square :array1.. 9ofboolean:= init(false, false, false, false, false, false, false, false, false)% Row/Column/Diagonal cell check
font :=Font.New("Times New Roman:14:bold")
fontWins :=Font.New("Times New Roman:12")
/* Draws X picture then saves it in a variable */ Draw.ThickLine(5, 5, 95, 95, 3, 16) Draw.ThickLine(5, 95, 95, 5, 3, 16)
picX :=Pic.New(0, 0, 100, 100)
Posted: Fri Mar 09, 2012 5:25 pm Post subject: RE:Tic-Tac-Toe
Useless without giving us the pictures
Amarylis
Posted: Fri Mar 09, 2012 9:24 pm Post subject: RE:Tic-Tac-Toe
Uhm... It creates it's own graphics....
The graphics in itself are pretty basic, it's just an O created from two Draw.FillOval's and the X is just two Draw.ThickLines, it's the AI that is it's main feature
Aange10
Posted: Fri Mar 09, 2012 9:30 pm Post subject: RE:Tic-Tac-Toe
Oh, I'm sorry. I didn't test the code; I saw the Pic.Draws and concluded you forgot to add the pictures. Most submissions who use Pic.Draw don't usually make their own pictures.
Amarylis
Posted: Fri Mar 09, 2012 9:40 pm Post subject: RE:Tic-Tac-Toe
Yeah, I didn't have patience to look for graphics, so I just used the default ones that the teacher gave the class
klutzedufus
Posted: Tue Mar 20, 2012 9:41 pm Post subject: RE:Tic-Tac-Toe
I'd just like to point out that if you get two rows complete in one game (using the same move), you get two wins added, as well as one tie. Of course, this isn't possible if you're playing against the AI. Looks great otherwise. And perhaps you could have a menu or something with the option to click on one player or two player as opposed to changing the variable in the code. Great work on the AI
Amarylis
Posted: Tue Mar 20, 2012 9:45 pm Post subject: RE:Tic-Tac-Toe
Yeah, the project was something for class, and I finished it WAY ahead of everyone else in the class, and no one would play with me.... So I made Steve. It was just so I could play by myself, didn't think I'd actually end up posting it anywhere, or have anyone else really use it. I might end up remaking this eventually one day when I'm really bored, but other than that I'm pretty much abandoning the project
TRUEAnonymous
Posted: Tue May 01, 2012 3:49 pm Post subject: RE:Tic-Tac-Toe
Woah the code is long, also a pretty good coding you got there
Sponsor Sponsor
Amarylis
Posted: Tue May 01, 2012 5:25 pm Post subject: RE:Tic-Tac-Toe
Why thank you :3
Raknarg
Posted: Tue May 01, 2012 8:00 pm Post subject: RE:Tic-Tac-Toe
Not a very elegant solution.
You should use an algorithm instead of a billion ifs.
Amarylis
Posted: Tue May 01, 2012 8:03 pm Post subject: RE:Tic-Tac-Toe
I didn't know of any that I could use for that purpose, didn't feel like looking it up. It seemed, at the time, the best thing to do for just a simple game. Do any come to mind for you?
Raknarg
Posted: Tue May 01, 2012 8:14 pm Post subject: RE:Tic-Tac-Toe
I could explain the basics.
There's two that come to mind, neither which I'm too familiar with, so you'd have to look them up later.
The first is basically a resursive check. It picks a spot, and gives the move a score according to what it does. In a basic game like this, usually you'll just have two values: 1 for a good move, -1 for a bad move. You then decide which move to use based on the resulting scores. The best score gets the move. That's what they do in games such as chess, they score moves according to what happens.
The second is weird, but I've heard it works. It similar, but theres no recursion. Essentially, it decides what to do based on the center square, and the surrounding pieces. It looks for patterns. After it checks one edge, you rotate the entire board and check again. You keep doing this uuntil all the sides have been checked, and you pick one of those options.
Amarylis
Posted: Tue May 01, 2012 8:16 pm Post subject: RE:Tic-Tac-Toe
I took a look at the "MiniMax Algorithm", and I'm having a bit of a problem understanding the pseudocode that was provided... Granted, I usually don't actually understand the pseudocode, but that's another issue entirely.
Here's the one that I found.
code:
function alphabeta(node, depth, a, b, Player)
if depth = 0 or node is a terminal node
return the heuristic value of node
if Player = MaxPlayer
for each child of node
a := max(a, alphabeta(child, depth-1, a, b, not(Player) ))
if b <= a
break (* Beta cut-off *)
return a
else
for each child of node
b := min(b, alphabeta(child, depth-1, a, b, not(Player) ))
if b <= a
break (* Alpha cut-off *)
return b
(* Initial call *)
alphabeta(origin, depth, -infinity, +infinity, MaxPlayer)
Raknarg
Posted: Wed May 02, 2012 8:45 pm Post subject: RE:Tic-Tac-Toe
Thats because this was made for people who are fairly proficient at programming.
Don't worry, I don't get it either. I'm sure they have better explanations somewhere.
Raknarg
Posted: Thu May 03, 2012 1:43 pm Post subject: RE:Tic-Tac-Toe
Ok, so I can't exactly understand how that code works, but I understand the algorithm.
Essentially, you just go through each possible path and determine a direction by the possible outcomes.
In tic tac toe, you would just go through each open space in order, and rate the move by the amount of wins. So you just recursively go through the whole field until no moves are left or until someone wins. I think a loss results -1, a tie results 0 and a win results 1. You add them up for each space and then pick the space with the best score.