Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   Code Gallery (https://forums.graalonline.com/forums/forumdisplay.php?f=179)
-   -   TicTacToe (https://forums.graalonline.com/forums/showthread.php?t=134256642)

Pelikano 10-24-2009 10:04 AM

TicTacToe
 
PHP Code:

//#CLIENTSIDE
function onWeaponFired() {
  
enum {PLAYERCOMPUTEREND};
  
openBoard();
  
this.turn PLAYER;
  
this.board = {" "" "" ",
                
" "" "" "
                
" "" "" ",};
  
this.locs = {{01500150}, {1503000150}, {3004500150},
              {
0150150,300}, {150300150300}, {300450150300},
              {
0150300,450}, {150300300,450}, {300450300,450}};
  
this.speak = {"I will now do my fabolous move!""Watch and learn!""Feel the power of my move!""Ah... I see an opening!""You are notting, but a waste of my time!",
                
"You cannot beat me!""My move will destroy you!""Even my Grandma plays better than you""Step back, it's my move!""Even god couldn't beat me!"};
}
function 
openBoard() {
  new 
GuiWindowCtrl("Tic_Window") {
    
profile "GuiBlueWindowProfile";
    
width 470;
    
height 510;
    
screenwidth - ( width );
    
screenheight - ( height );
    
title canMove useownprofile destroyonhide visible true;
    
canResize canMaximize canMinimize canClose false;
    
profile.transparency .9;
    
text "Tic Tac Toe";
    new 
GuiFrameSetCtrl("Tic_Frames") {
      
columncount 3;
      
rowcount 3;
      
10;
      
25;
      
width 450;
      
height 450;
      
bordermovable "alwaysOff";
      
bordercolor = {1,1,0};
    }
    new 
GuiMLTextCtrl("Tic_Text") {
      
profile GuiBlueMLTextProfile;
      
10;
      
480;
      
width 300;
      
height 20;
      
text "<font color=green><b>Welcome to Tic Tac Toe! (Made by Sam)</font></b>";
    }
    new 
GuiButtonCtrl("Tic_Reset") {
      
profile GuiBlueButtonProfile;
      
260;
      
480;
      
width 100;
      
height 25;
      
text "Reset";
    }
    new 
GuiButtonCtrl("Tic_Exit") {
      
profile GuiBlueButtonProfile;
      
365;
      
480;
      
width 100;
      
height 25;
      
text "Exit";
    }
  }
}
function 
Tic_Exit.onAction() {
  
Tic_Window.destroy();
}
function 
Tic_Reset.onAction() {
  
Tic_Window.destroy();
  
onWeaponFired();
}
function 
ChangeText(colortextextraTime) {
  
cancelevents("ChangeBack");
  
Tic_Text.text "<font color=" color "><b>" text "</font></b>";
  
scheduleevent(1.5 + (extraTime!=NULL?extraTime:0), "ChangeBack"NULL);
}
function 
onChangeBack() {
  
Tic_Text.text "<font color=green><b>Welcome to Tic Tac Toe! (Made by Sam)</font></b>";
}
function 
Tic_Frames.onMouseDown(keymodifiermousescreenxmousescreeny) {
  if (
this.turn == PLAYER) {
    
temp.click Tic_Frames.globaltolocalcoord({mousescreenxmousescreeny});
    for (
09i++) {
      if (
click[0] > this.locs[i][0] && click[0] < this.locs[i][1] && click[1] > this.locs[i][2] && click[1] < this.locs[i][3]) {
        if (
isLegal(1)) {
          
ChangeText("green""Move accepted!");
          
Moved(1);
        }
        else {
          
ChangeText("red""This move it not legal!");
        }
        return; 
      }
    }
  }
  elseif (
this.turn == COMPUTER) {
    
ChangeText("red""It is not your turn!");
  }
  else {
    
ChangeText("red""This game has ended already!");
  }
}
function 
Moved(myMove) {
  
updateBoard(myMove);
  
temp.winner gameEnd(this.turn);
  if (
winner == "player") {
    
ChangeText("green""You win!");
    
this.turn END;
  }
  else if (
winner == "computer") {
    
ChangeText("red""The Computer wins!");
    
this.turn END;
  }
  else if (
winner == "tie") {
    
ChangeText("green""This game ends in a Tie!");;
    
this.turn END;
  }
  if (
this.turn == PLAYER) {
    
this.turn COMPUTER;
    
Moved(generateTurn());
  }
  elseif (
this.turn == COMPUTER) {
    
this.turn PLAYER;
  }
}

function 
gameEnd(moverboard) {
  if (
board == NULLboard this.board;
  if (!(
" " in board)) return "tie";
  
  
temp.winChar = (mover==PLAYER?"X":"O");
  
temp.win = {
             {
1,2,3},
             {
4,5,6},
             {
7,8,9},
             {
1,4,7},
             {
2,5,8},
             {
3,6,9},
             {
1,5,9},
             {
3,5,7}
                   };
  for (
08i++) {
    if (
board[win[i][0] - 1] == winChar && board[win[i][1] - 1] == winChar && board[win[i][2] - 1] == winChar) {
      return (
winChar=="X"?"player":"computer");
    }
  }
  return 
"no one";
}

function 
isLegal(myMoveboard) {
  if (
board == NULLboard this.board;
  if (
board[myMove 1] == " ") {
    return 
true;
  }
  return 
false;
}

function 
generateTurn() {
   
// First checks if he can win, then checks if the player can win, if not then just choose a random move out of the bestMoves
   
ChangeText("yellow"this.speak[int(random(0this.speak.size()))], 1);
   
sleep(2);
   
temp.winners = {"computer""player"};
   
temp.fake_board this.board;
   for (
temp.02i++) {
     for (
myMove 1myMove 10myMove++) {
       if (!
isLegal(myMovefake_board)) continue;
       
fake_board[myMove 1] = (i==0?"O":"X");
       
temp.turn = (i==0?COMPUTER:PLAYER);
       
temp.end gameEnd(turnfake_board);
       if (
end == winners[i] || end == "tie") {
         return 
myMove;
       }
       
fake_board[myMove 1] = " ";
     }
   }
   
temp.bestMoves = {{5},{1,3,7,9},{4,6,2,8}};
   for (
temp.03i++) {
     
temp.tempsize bestMoves[i].size();
     for (
temp.0tempsizep++) {
       
temp.randmove int(random(0bestMoves[i].size()));
       if (!
isLegal(bestMoves[i][randmove])) {
         if (
bestMoves[i][p] != 5bestMoves[i].remove(randmove);
         continue;
       }
       return 
bestMoves[i][randmove];
     }
   }
}

function 
updateBoard(myMove) {
  
this.board[myMove 1] = (this.turn==PLAYER?"X":"O");
  
temp.img = (thiso.turn==PLAYER?"cross.gif":"kreis.gif");
  new 
GuiShowImgCtrl("Image_" myMove) {
    
thiso.locs[myMove 1][0] + 5;
    
thiso.locs[myMove 1][2] + 25;
    
width 150;
    
height 150;
    
image img;
  }
  
Tic_Window.addcontrol((@ "Image_" myMove));


A Tic Tac Toe game where the computer plays with you. (He is beatable)

I am pretty sure that there's a way easier way of adding those X and O images into the GUI also I think my AI scripting is pretty silly, so I'm looking for feedback!

Deas_Voice 10-24-2009 01:43 PM

there's a space at the //#CLIENTSIDE, remove it and u will be able to open it.

also, is it supposed to look like this?
http://i37.tinypic.com/2cffq8m.png

Pelikano 10-24-2009 02:38 PM

3 Attachment(s)
Quote:

Originally Posted by Deas_Voice (Post 1533634)
there's a space at the //#CLIENTSIDE, remove it and u will be able to open it.

also, is it supposed to look like this?
http://i37.tinypic.com/2cffq8m.png

oO no

It doesn't look like this for me, I'll upload a screen in a sec

of course you don't have the images for the X and O aswell

cbk1994 10-24-2009 02:53 PM

Why not just use a really big font and a GuiTextCtrl for the X and O (protip: even Paint can generate straight lines :noob:)?

Pelikano 10-24-2009 03:25 PM

Quote:

Originally Posted by cbk1994 (Post 1533645)
Why not just use a really big font and a GuiTextCtrl for the X and O (protip: even Paint can generate straight lines :noob:)?

straight lines are for noobs!!

but good idea

Liberated 10-24-2009 04:40 PM

So you made somesort of AI for the computer? Or just a fixed pattern?

Pelikano 10-24-2009 04:52 PM

Quote:

Originally Posted by Liberated (Post 1533675)
So you made somesort of AI for the computer? Or just a fixed pattern?

yeah the computer has some sort of AI

Switch 10-24-2009 10:06 PM

Delteria's is awesome :)


All times are GMT +2. The time now is 03:59 PM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.