Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Today's Posts

 
 
Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 10-24-2009, 10:04 AM
Pelikano Pelikano is offline
Registered User
Pelikano's Avatar
Join Date: Oct 2008
Posts: 1,133
Pelikano has a little shameless behaviour in the past
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!
Reply With Quote
 


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 06:20 PM.


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