Graal Forums  

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

Reply
 
Thread Tools Search this Thread Display Modes
  #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
  #2  
Old 10-24-2009, 01:43 PM
Deas_Voice Deas_Voice is offline
Deas
Deas_Voice's Avatar
Join Date: Jun 2007
Location: Sweden
Posts: 2,264
Deas_Voice is a jewel in the roughDeas_Voice is a jewel in the rough
Send a message via AIM to Deas_Voice Send a message via MSN to Deas_Voice Send a message via Yahoo to Deas_Voice
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?
__________________
.
WTF is real life, and where do I Download it?
There is no Real Life, just AFK!
since 2003~
I Support~
ღAeonღ | ღTestbedღ | ღDelteriaღ

if you are going to rep me, don't be an idiot, leave your name!
I got nothing but love for you
Reply With Quote
  #3  
Old 10-24-2009, 02:38 PM
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
Quote:
Originally Posted by Deas_Voice View Post
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?
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
Attached Thumbnails
Click image for larger version

Name:	TicTacToe.jpg
Views:	172
Size:	62.8 KB
ID:	49682  
Attached Images
  
Reply With Quote
  #4  
Old 10-24-2009, 02:53 PM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Why not just use a really big font and a GuiTextCtrl for the X and O (protip: even Paint can generate straight lines )?
__________________
Reply With Quote
  #5  
Old 10-24-2009, 03:25 PM
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
Quote:
Originally Posted by cbk1994 View Post
Why not just use a really big font and a GuiTextCtrl for the X and O (protip: even Paint can generate straight lines )?
straight lines are for noobs!!

but good idea
Reply With Quote
  #6  
Old 10-24-2009, 04:40 PM
Liberated Liberated is offline
not doing alot
Liberated's Avatar
Join Date: Feb 2008
Posts: 1,366
Liberated has a spectacular aura about
So you made somesort of AI for the computer? Or just a fixed pattern?
__________________
Quote:
Originally Posted by Tigairius View Post
I promise when I get rich I'll send you an iPhone. I'll send everyone an iPhone.
Reply With Quote
  #7  
Old 10-24-2009, 04:52 PM
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
Quote:
Originally Posted by Liberated View Post
So you made somesort of AI for the computer? Or just a fixed pattern?
yeah the computer has some sort of AI
Reply With Quote
  #8  
Old 10-24-2009, 10:06 PM
Switch Switch is offline
o.o
Switch's Avatar
Join Date: Jan 2007
Location: Philadelphia
Posts: 3,038
Switch has a spectacular aura about
Send a message via MSN to Switch
Delteria's is awesome
__________________
Oh squiggly line in my eye fluid. I see you lurking there on the peripheral of my vision.
But when I try to look at you, you scurry away.
Are you shy, squiggly line?
Why only when I ignore you, do you return to the center of my eye?
Oh, squiggly line, it's alright, you are forgiven.
Reply With Quote
Reply


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 12:00 AM.


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