//-------------------------------------------// // This is the script for the tictactoe map. // //-------------------------------------------// //The following definitions section defines values for constants that will be used throughout this script. #ifndef __TICTACTOE_DEFS__ #define __TICTACTOE_DEFS__ #define FALSE 0 #define TRUE 1 #define NULL 0 #define MAGIC_NUMBER 15 #define ONE 1 #define TWO 2 #define THREE 3 #define FOUR 4 #define FIVE 5 #define SIX 6 #define SEVEN 7 #define EIGHT 8 #define NINE 9 #define ZERO 0 #endif //Predeclaration of needed functions. float checkSpot(float spot); void setPlacement(float button); //Global Variables. float a = NULL; float x = NULL; float b = NULL; float y = NULL; float c = NULL; float z = NULL; float d = NULL; float w = NULL; float e = NULL; //Functions void interact() { $interactive.show(); sys.wait(0.1); $noninteractive.hide(); } void noninteract() { $noninteractive.show(); sys.wait(0.1); $interactive.hide(); } void moveBot() { aiScriptedTetherMoveWait($aiBot, $tetherPlay); $aiBot.lookAt($aiLook); interact(); } void spotTaken() { sys.println("That spot is already occupied, please choose another."); } float checkForWin(float num1, float num2, float num3) { //Sets the default state for winner to false. float winner = FALSE; //Adds up the numbers that have been passed in to determine whether the game has been won or not. float total = num1 + num2 + num3; //If the sum of the numbers is 15, then we have a winner and winner is set to true. if (total == MAGIC_NUMBER) { winner = TRUE; } //This returns winner, if it's true, then the game should end. return winner; } void placeMaiden(float spot) { //Checks to see where the computer finally decided to place the maiden, places maiden there. if (spot == ONE) sys.trigger($enemySpawn1); else if (spot == TWO) sys.trigger($enemySpawn2); else if (spot == THREE) sys.trigger($enemySpawn3); else if (spot == FOUR) sys.trigger($enemySpawn4); else if (spot == FIVE) sys.trigger($enemySpawn5); else if (spot == SIX) sys.trigger($enemySpawn6); else if (spot == SEVEN) sys.trigger($enemySpawn7); else if (spot == EIGHT) sys.trigger($enemySpawn8); else if (spot == NINE) sys.trigger($enemySpawn9); else sys.println("Error within placeMaiden function: no value found."); } void aiChoose() { //Makes a random integer between 1 and 9, assigns it to compChoice float compChoice = sys.randomInt(EIGHT) + ONE; //Makes a random wait time for the computer between 1 and 2 float pretendWait = sys.random(ONE) + ONE; //Checks to see if compChoice is valid, if it is not valid then it calls the function again if (checkSpot(compChoice) == FALSE) { setPlacement(compChoice); sys.wait(pretendWait); aiScriptedAnimWait($aiBot, "point_forward", 16, 1); placeMaiden(compChoice); } else aiChoose(); } void gameOver() { sys.wait(THREE); sys.trigger($killAll); } void playGame() { float playerWin = FALSE; float aiWin = FALSE; //If a, b, and c have been chosen, check to see if the player has won. if (a != NULL && b != NULL && c != NULL) { if (checkForWin(a, b, c) == TRUE) playerWin = TRUE; } //If a, b, c, and d have been chosen, check to see if the player has won. if (a != NULL && b != NULL && c != NULL && d != NULL) { if (checkForWin(a, b, d) == TRUE) playerWin = TRUE; else if (checkForWin(a, d, c) == TRUE) playerWin = TRUE; else if (checkForWin(d, b, c) == TRUE) playerWin = TRUE; } //If a, b, c, d, and e have been chosen, check to see if the player has won. if (a != NULL && b != NULL && c != NULL && d != NULL && e != NULL) { if (checkForWin(a, b, e) == TRUE) playerWin = TRUE; else if (checkForWin(a, e, c) == TRUE) playerWin = TRUE; else if (checkForWin(e, b, c) == TRUE) playerWin = TRUE; else if (checkForWin(a, d, e) == TRUE) playerWin = TRUE; else if (checkForWin(d, b, e) == TRUE) playerWin = TRUE; else if (checkForWin(d, c, e) == TRUE) playerWin = TRUE; } if (playerWin == TRUE) { sys.println("You have won the game!"); gameOver(); } else { //If all spots are taken and it gets here, then the cat got the game. if (a != NULL && b != NULL && c != NULL && d != NULL && e != NULL && x != NULL && y != NULL && z != NULL && w != NULL) { sys.println("It looks like the cat got this one!"); gameOver(); } else //Calls function for the computer to choose a spot. aiChoose(); //If x, y, and z have been chosen, check to see if the computer has won. if (x != NULL && y != NULL && z != NULL) { if (checkForWin(x, y, z) == TRUE) aiWin = TRUE; } //If x, y, z, and w have been chosen, check to see if the computer has won. if (x != NULL && y != NULL && z != NULL && w != NULL) { if (checkForWin(x, y, w) == TRUE) aiWin = TRUE; else if (checkForWin(x, w, z) == TRUE) aiWin = TRUE; else if (checkForWin(w, y, z) == TRUE) aiWin = TRUE; } if (aiWin == TRUE) { sys.println("You have been defeated by the Strogg!"); gameOver(); } } } void setPlacement(float button) { //This long if-else statement determines which variable should be set to the button number entered either by the player or the AI. if (a == NULL) { a = button; } else if (x == NULL) { x = button; } else if (b == NULL) { b = button; } else if (y == NULL) { y = button; } else if (c == NULL) { c = button; } else if (z == NULL) { z = button; } else if (d == NULL) { d = button; } else if (w == NULL) { w = button; } else if (e == NULL) { e = button; } else { sys.println("System error in setPlacement()."); } /* //debug sys.println("A = " + a); sys.println("B = " + b); sys.println("C = " + c); sys.println("D = " + d); sys.println("E = " + e); sys.println("X = " + x); sys.println("Y = " + y); sys.println("Z = " + z); sys.println("W = " + w); */ } float checkSpot(float spot) { float isFull = FALSE; if (a == spot || b == spot || c == spot || d == spot || e == spot || w == spot || x == spot || y == spot || z == spot) { isFull = TRUE; } return isFull; } //These are the GUI functions... void buttonOne() { if (checkSpot(ONE) == FALSE) { noninteract(); setPlacement(ONE); sys.trigger($playerSpawn1); playGame(); interact(); } else spotTaken(); } void buttonTwo() { if (checkSpot(TWO) == FALSE) { noninteract(); setPlacement(TWO); sys.trigger($playerSpawn2); playGame(); interact(); } else spotTaken(); } void buttonThree() { if (checkSpot(THREE) == FALSE) { noninteract(); setPlacement(THREE); sys.trigger($playerSpawn3); playGame(); interact(); } else spotTaken(); } void buttonFour() { if (checkSpot(FOUR) == FALSE) { noninteract(); setPlacement(FOUR); sys.trigger($playerSpawn4); playGame(); interact(); } else spotTaken(); } void buttonFive() { if (checkSpot(FIVE) == FALSE) { noninteract(); setPlacement(FIVE); sys.trigger($playerSpawn5); playGame(); interact(); } else spotTaken(); } void buttonSix() { if (checkSpot(SIX) == FALSE) { noninteract(); setPlacement(SIX); sys.trigger($playerSpawn6); playGame(); interact(); } else spotTaken(); } void buttonSeven() { if (checkSpot(SEVEN) == FALSE) { noninteract(); setPlacement(SEVEN); sys.trigger($playerSpawn7); playGame(); interact(); } else spotTaken(); } void buttonEight() { if (checkSpot(EIGHT) == FALSE) { noninteract(); setPlacement(EIGHT); sys.trigger($playerSpawn8); playGame(); interact(); } else spotTaken(); } void buttonNine() { if (checkSpot(NINE) == FALSE) { noninteract(); setPlacement(NINE); sys.trigger($playerSpawn9); playGame(); interact(); } else spotTaken(); } //...this ends the GUI function section void main() { //Nothing to see here. Move along. }