PageMac
Home
   dot Projects
   dot Pics and Images
   dot WREK
D++
   dot About / FAQ
   dot Download
   dot Code Library
   dot Documentation
   dot VB6 Runtime Files
About

  Source Code - dpp/source/tictactoe.dpp

Download link (right click, save as)



1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
'TicTacToe.dpp - by Azure (www.pagemac.com)
'Not the greatest, but hey - tic tac toe is tic tac toe,
'even if it doesn't have fancy graphics. :)

newvar array[8];

function main()
{
    newvar noWinner=true;
    newvar playerInput, playerTurn = 1;
    
    screenput "Tic Tac Toe!"; screen;
    displayBoard();

    do while boardEmpty() && noWinner;
        if playerTurn = 1 then
            screenput dpp.crlf & "Enter X Please: ";
        else
            screenput dpp.crlf & "Enter O Please: ";
        endif
        screenin playerInput;
        
        if playerInput < 1 || playerInput > 9 then
            screenput " <- Invalid move" & dpp.crlf;
        elseif array[playerInput-1] <> 0 then
            screenput " <- Invalid move" & dpp.crlf;
        else
            array[playerInput-1] = playerTurn; 
            if playerTurn = 1 then
                playerTurn = 2;
            else
                playerTurn = 1;
            endif
        endif
        screen;
        displayBoard();
        noWinner = checkWinner();
     loop
     if boardEmpty() = false then
        screenput "DRAW!"; screen;
     endif
     screen;        
}

function displayBoard()
{
    newvar i=0,j=1;
    screen;
    for i = 0 to 8;
        screenput " ";
        if array[i] = 1 then
            screenput "X";
        elseif array[i] = 2 then
            screenput "O";
        else
            screenput i + 1;
        endif
        if j=3 then
            screen; j=0;
        endif
        j++;
    next i;
}

function boardEmpty()
{
    newvar flag, i;
    
    for i=0 to 8;
        if array[i] = 0 then 
            flag = true;
            exit_for;
        endif
    next i;
    
    return flag;
}

function checkWinner()
{
    newvar noWinner = true;
    if (array[0] =1 && array[1]=1 && array[2]=1) ||
    (array[0]=1 && array[3]=1 && array[6]=1) ||
    (array[3]=1 && array[4]=1 && array[5]=1) ||
    (array[6]=1 && array[7]=1 && array[8]=1) ||
    (array[1]=1 && array[4]=1 && array[7]=1) ||
    (array[2]=1 && array[5]=1 && array[8]=1) ||
    (array[0]=1 && array[4]=1 && array[8]=1) ||
    (array[2]=1 && array[4]=1 && array[6]=1) then
        screenput dpp.crlf & "X is the Winner!"; screen;
        noWinner = false;
    endif
        
    if (array[0]=2 && array[1]=2 && array[2]=2) ||
    (array[0]=2 && array[3]=2 && array[6]=2) ||
    (array[3]=2 && array[4]=2 && array[5]=2) ||
    (array[6]=2 && array[7]=2 && array[8]=2) ||
    (array[1]=2 && array[4]=2 && array[7]=2) ||
    (array[2]=2 && array[5]=2 && array[8]=2) || 
    (array[0]=2 && array[4]=2 && array[8]=2) ||
    (array[2]=2 && array[4]=2 && array[6]=2) then
        screenput dpp.crlf & "O is the Winner!"; screen;
        noWinner = false;
    endif
    return noWinner;
}




This code is Copyright (C) 2000-2012 PageMac Programming. Reproduction of this code must adhere to the GNU General Public License.