Shoutbox

Tic-Tac-Toe - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: General (/forumdisplay.php?fid=11)
+---- Forum: General Chit Chat (/forumdisplay.php?fid=14)
+----- Thread: Tic-Tac-Toe (/showthread.php?tid=53438)

Tic-Tac-Toe by saralk on 11-29-2005 at 03:46 PM

I made a game of tic-tac-toe in computing today!

Its pretty cool, you can play against a computer, although the computer is very easy to beat, there is a severe lack of interface and there are a few debug things left over.

this is the code to it:

code:
program tictactoe;
{Saral Kaushik}
{$apptype console}

var
   Board : array [1..3, 1..3] of char;
   Turn : integer;
   PlayGame : boolean;

procedure SetBoard;
var
   SetLoop : integer;
   SetLoop2 : integer;
begin
     for SetLoop := 1 to 3 do
         for SetLoop2 := 1 to 3 do
             Board[SetLoop, SetLoop2] := '.';
end;

procedure DisplayBoard;
var
   DisplayLoop : integer;
   DisplayLoop2 : integer;
begin
     writeln('1 2 3');
     for DisplayLoop := 1 to 3 do
     begin
         for DisplayLoop2 := 1 to 3 do
             write(Board[DisplayLoop, DisplayLoop2],' ');
         writeln(DisplayLoop,' ');
     end;
end;

function PlayerTurn(Character : char):boolean;
var
   XPosition : integer;
   YPosition : integer;
   Move : boolean;
begin
     Move := FALSE;
     while Move = FALSE do
     begin
           write('Enter X Position: ');
           readln(XPosition);
           write('Enter Y Position: ');
           readln(YPosition);
           if Board [YPosition, XPosition] = '.' then
           begin
              Board [YPosition, XPosition] := Character;
              Move := TRUE;
           end
           else
               writeln('Position Taken');
     end;
end;

function BoardFull():boolean;
var
   FullLoop : integer;
   FullLoop2 : integer;
   Total : integer;
begin
     Total := 0;
     for FullLoop := 1 to 3 do
         for FullLoop2 := 1 to 3 do
             if (Board[FullLoop, FullLoop2] <> '.') then Total := Total + 1;
     if (Total = 9) then BoardFull := TRUE
     else BoardFull := FALSE;
end;

function IsWin(CheckFor:char):boolean;
const
     WinString = '111000000000111000000000111100010001001010100100100100010010010001001001';
var
   CheckLoopX : integer;
   CheckLoopY : integer;
   MatchCount : integer;
   StringNo : integer;
begin
     MatchCount := 0;
     StringNo := 1;
     repeat
          for CheckLoopX := 1 to 3 do
              for CheckLoopY := 1 to 3 do
                  begin
                  if ((Board [CheckLoopX, CheckLoopY] = CheckFor) and (WinString[StringNo] = '1')) then
                     MatchCount := MatchCount + 1;
                  //writeln (CheckLoopX, ' ',CheckLoopY,' ',Board[CheckLoopX, CheckLoopY],' ',WinString[StringNo],' ',MatchCount);
                  StringNo := StringNo + 1;
                  end;
          //writeln;
          if (MatchCount < 3) then MatchCount := 0;

     until (MatchCount >= 3) or (StringNo >= 72);
     if MatchCount >= 3 then begin
     IsWin := TRUE; //writeln('Win')
     end
     else IsWin := FALSE;
end;

function MakeMove(Player:char):boolean;
const
     MoveString = '112000000000112000000000112211000000000211000000000211121000000000121000000000121100100200010010020001001002200100100020010010002001001100200100010020010001002001100010002200010001100020001002010100001010200001020100';
var
   CheckLoopX : integer;
   CheckLoopY : integer;
   MatchCount : integer;
   StringNo : integer;
   MoveX : integer;
   MoveY : integer;
   OtherPlayer : char;
begin
     //check to see if a win is possible
     MatchCount := 0;
     StringNo := 1;
     MoveX := 0;
     MoveY := 0;
     repeat
           for CheckLoopX := 1 to 3 do
           begin
               for CheckLoopY := 1 to 3 do
                   begin
                        if (MoveString [StringNo] = '2') and (Board [CheckLoopX, CheckLoopY] = '.') then
                        begin
                             MoveX := CheckLoopX;
                             MoveY := CheckLoopY;
                        end;
                        if (MoveString[StringNo] = '1') and (Board [CheckLoopX, CheckLoopY] = Player) then
                        begin
                             MatchCount := MatchCount + 1;
                             //writeln('matched');
                             //readln;
                        end;
                        //writeln(StringNo,' ',Board[CheckLoopX, CheckLoopY],' ',MoveString[StringNo],' ',MatchCount);
                        StringNo := StringNo + 1;
                   end;

           end;
           if (MatchCount < 2) then MatchCount := 0;
           //writeln;
     until (((MatchCount >= 2) and (MoveX <> 0)) or (StringNo >= 216));
     if ((MatchCount >= 2) and (MoveX <> 0)) then
     begin
     Board [MoveX, MoveY] := Player;
     writeln('Win Possible Move Made');
     end
     else
     begin
          //check to see if a block is possible
          MatchCount := 0;
          StringNo := 1;
          repeat
                for CheckLoopX := 1 to 3 do
                for CheckLoopY := 1 to 3 do
                begin
                     if (MoveString [StringNo] = '2') and (Board [CheckLoopX, CheckLoopY] = '.') then
                     begin
                          MoveX := CheckLoopX;
                          MoveY := CheckLoopY;
                     end;
                     if (Player = 'X') then OtherPlayer := 'O'
                     else OtherPlayer := 'X';
                     if (MoveString[StringNo] = '1') and (Board [CheckLoopX, CheckLoopY] = OtherPlayer) then MatchCount := MatchCount + 1;
                     //writeln(StringNo,' ',Board[CheckLoopY, CheckLoopX],' ',MoveString[StringNo],' ',MatchCount);
                     StringNo := StringNo + 1;
                end;
                if (MatchCount < 2) then MatchCount := 0;
          until (((MatchCount >= 2) and (MoveX <> 0)) or (StringNo >= 216));
          if (MatchCount >= 2) and (MoveX <> 0) then
          begin
          Board [MoveX, MoveY] := Player;
          writeln('Block Possible Move Made', MatchCount);
          end
          else
              begin
              repeat
                    randomize;
                    MoveX := Random(4);
                    MoveY := Random(4);
              until (Board [MoveX, MoveY] = '.');
              Board [MoveX, MoveY] := Player;
              writeln('Random Move Made');
              end;
     end;
end;

begin
     SetBoard;
     DisplayBoard;
     begin
     Turn := 1;
     PlayGame := TRUE;
     repeat
           if Turn = 1 then
           begin
                writeln('Player 1');
                PlayerTurn('X');
                DisplayBoard;
                Turn := 0;
           end
           else
           begin
                {writeln('Player 2');
                PlayerTurn('O');
                DisplayBoard;
                if IsWin('O') then
                begin
                     writeln('Player 2 Wins');
                     PlayGame := FALSE;
                end;}
                MakeMove('O');
                DisplayBoard;
                Turn := 1;
           end;
           if (IsWin('X') = TRUE) then writeln('Human Wins, Well Done Human')
           else if (IsWin('O') = TRUE) then writeln('Computer Wins, Better Luck Next Time Human');
     until ((BoardFull = TRUE) or (PlayGame = FALSE));
     end;
     readln;
end.

RE: Tic-Tac-Toe by Rubber Stamp on 11-29-2005 at 03:50 PM

c++? its good timepass..lol


RE: Tic-Tac-Toe by CookieRevised on 11-29-2005 at 04:10 PM

quote:
Originally posted by Rubber Stamp
c++?
Hexview it and take a calculated guess: Borland Delphi

RE: Tic-Tac-Toe by Rubber Stamp on 11-29-2005 at 04:23 PM

what?? sorry i dont get you cookie.
* Rubber Stamp feels soo stupid!


RE: Tic-Tac-Toe by CookieRevised on 11-29-2005 at 04:28 PM

After opening the executable in my hexviewer and seeing some stuff, I think it is written in Borland Delphi, not C++


As for "Hex-viewing" or "Hex-editing", see:
* CookieRevised's reply to [Tut-Updated 08/04/05] Resource Hacking - Hex Editing
* CookieRevised's reply to Help with understanding.


RE: Tic-Tac-Toe by user27089 on 11-29-2005 at 04:37 PM

That game is amazing!!! I keep winning ;p.


Edit:
[Image: attachment.php?pid=571387]


RE: Tic-Tac-Toe by MeEtc on 11-29-2005 at 05:55 PM

cool!
If you're not too protective, and it is in Delphi, could you send the source along? I like toying with Delphi.

I might fix the look up a bit :P


RE: Tic-Tac-Toe by John Anderton on 11-29-2005 at 06:04 PM

quote:
Originally posted by traxor

Attachment: I WON!.JPG (28.03 KB)
This file has been downloaded 17 time(s).

This post was edited Today at 10:10 PM by traxor.
Thats how i won too :P But it still asks for my input for the one remaining space 8-)
saral .....
RE: Tic-Tac-Toe by Negro_Joe on 11-29-2005 at 06:09 PM

lol..i enjoyed that for the 10 seconds i was playing on it...:P


RE: Tic-Tac-Toe by saralk on 11-29-2005 at 06:21 PM

quote:
Originally posted by John Anderton
quote:
Originally posted by traxor

Attachment: I WON!.JPG (28.03 KB)
This file has been downloaded 17 time(s).

This post was edited Today at 10:10 PM by traxor.
Thats how i won too :P But it still asks for my input for the one remaining space 8-)
saral .....

yeah, I know about that bug, I just need to change it around a bit. I got a bit over excited and forgot that i still had that left to do.

quote:
Originally posted by MeEtc
cool!
If you're not too protective, and it is in Delphi, could you send the source along? I like toying with Delphi.

I might fix the look up a bit :P

i'll post the code next time i'm in school. Edit: Just realised I can access my school folder from home, see above

quote:
Originally posted by CookieRevised
quote:
Originally posted by Rubber Stamp
c++?
Hexview it and take a calculated guess: Borland Delphi

correct, as ever!
RE: Tic-Tac-Toe by MeEtc on 11-29-2005 at 06:37 PM

WOW, you might want to put that as an attachment on your previous post instead.

a bit long!