| Pelikano |
10-24-2009 10:04 AM |
TicTacToe
PHP Code:
//#CLIENTSIDE
function onWeaponFired() {
enum {PLAYER, COMPUTER, END};
openBoard();
this.turn = PLAYER;
this.board = {" ", " ", " ",
" ", " ", " ",
" ", " ", " ",};
this.locs = {{0, 150, 0, 150}, {150, 300, 0, 150}, {300, 450, 0, 150},
{0, 150, 150,300}, {150, 300, 150, 300}, {300, 450, 150, 300},
{0, 150, 300,450}, {150, 300, 300,450}, {300, 450, 300,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;
x = screenwidth / 2 - ( width / 2 );
y = screenheight / 2 - ( height / 2 );
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;
x = 10;
y = 25;
width = 450;
height = 450;
bordermovable = "alwaysOff";
bordercolor = {1,1,0};
}
new GuiMLTextCtrl("Tic_Text") {
profile = GuiBlueMLTextProfile;
x = 10;
y = 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;
x = 260;
y = 480;
width = 100;
height = 25;
text = "Reset";
}
new GuiButtonCtrl("Tic_Exit") {
profile = GuiBlueButtonProfile;
x = 365;
y = 480;
width = 100;
height = 25;
text = "Exit";
}
}
}
function Tic_Exit.onAction() {
Tic_Window.destroy();
}
function Tic_Reset.onAction() {
Tic_Window.destroy();
onWeaponFired();
}
function ChangeText(color, text, extraTime) {
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(keymodifier, mousescreenx, mousescreeny) {
if (this.turn == PLAYER) {
temp.click = Tic_Frames.globaltolocalcoord({mousescreenx, mousescreeny});
for (i = 0; i < 9; i++) {
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(i + 1)) {
ChangeText("green", "Move accepted!");
Moved(i + 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(mover, board) {
if (board == NULL) board = 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 (i = 0; i < 8; i++) {
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(myMove, board) {
if (board == NULL) board = 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(0, this.speak.size()))], 1);
sleep(2);
temp.winners = {"computer", "player"};
temp.fake_board = this.board;
for (temp.i = 0; i < 2; i++) {
for (myMove = 1; myMove < 10; myMove++) {
if (!isLegal(myMove, fake_board)) continue;
fake_board[myMove - 1] = (i==0?"O":"X");
temp.turn = (i==0?COMPUTER:PLAYER);
temp.end = gameEnd(turn, fake_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.i = 0; i < 3; i++) {
temp.tempsize = bestMoves[i].size();
for (temp.p = 0; p < tempsize; p++) {
temp.randmove = int(random(0, bestMoves[i].size()));
if (!isLegal(bestMoves[i][randmove])) {
if (bestMoves[i][p] != 5) bestMoves[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) {
x = thiso.locs[myMove - 1][0] + 5;
y = 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!
|