if the light controller have time to play
Posted: Sat Sep 28, 2013 12:01 pm
a MADRIX MAS script for play tic tac toe
X | O | X
O | X | O
X | O | X
let's play
X | O | X
O | X | O
X | O | X
Code: Select all
@scriptname="tic tac toe";
@author="dieter";
@version="09/28/2013";
@description="MAS script for light operator to play tic tac toe";
// special thank to Dinesh Smokin
// http://www.codeincodeblock.com/2011/06/mini-project-tic-tac-game-source-code.html
int board_init[] = {2,2,2,2,2,2,2,2,2,2};
int board[] = board_init;
int turn = 1, flag = 0;
int player,comp;
int player_winXO = -1;
int blinking = 0;
int choice_last = -1;
int input_value = 0;
int input_changed = 0;
color matrix[][];
color Cross = BLUE, Oooo = RED, Draw = GREEN;
ctrlbutton2 cb = {"play O", "OnPlayO", "play X", "OnPlayX"};
ctrlbutton3 cb1 = {"-", "OnOX11", "-", "OnOX12", "-", "OnOX13"};
ctrlbutton3 cb2 = {"-", "OnOX21", "-", "OnOX22", "-", "OnOX23"};
ctrlbutton3 cb3 = {"-", "OnOX31", "-", "OnOX32", "-", "OnOX33"};
ctrledit ce = {"Info", EDIT_ALIGN_LEFT, "choice play X or play O"};
void InitEffect(){
Clear();
blinking=0;
}
void RenderEffect(){
switch(player_winXO){
case 0: // computer win
case 1: // player win
if(choice_last==player_winXO) blink(Oooo);
else blink(Cross);
break;
case 2: blink(Draw); break; // draw
default:if(choice_last!=-1)player_first(); break; // play
}
}
void MatrixSizeChanged(){InitEffect();}
void OnPlayO(){InitEffect();menu(0);} // play O
void OnPlayX(){InitEffect();menu(1);} // play X
void OnOX11(){input_value=1;input_changed=1;}
void OnOX12(){input_value=2;input_changed=1;}
void OnOX13(){input_value=3;input_changed=1;}
void OnOX21(){input_value=4;input_changed=1;}
void OnOX22(){input_value=5;input_changed=1;}
void OnOX23(){input_value=6;input_changed=1;}
void OnOX31(){input_value=7;input_changed=1;}
void OnOX32(){input_value=8;input_changed=1;}
void OnOX33(){input_value=9;input_changed=1;}
void blink(color blink_color){
if(blinking==0) GetPixelArea(matrix); // save
if(blinking>9) DrawPixelArea(matrix); // redraw
else Clear(blink_color);
blinking++;
blinking=blinking%20;
}
void menu(int choice){
cb.label1="replay O";
cb.label2="replay X";
choice_last=choice;
player_winXO=-1;
board = board_init;
turn = 1;
flag = 0;
if(choice==1){ // player play X
makeLabels(1); // X
player = 1;
comp = 0;
player_first();}
else{ // player play O
makeLabels(0); // O
player = 0;
comp = 1;
start_game();}
}
void makeLabels(int XO){
string text="O";
if(XO==1)text="X";
cb1.label1 = text;
cb1.label2 = text;
cb1.label3 = text;
cb2.label1 = text;
cb2.label2 = text;
cb2.label3 = text;
cb3.label1 = text;
cb3.label2 = text;
cb3.label3 = text;
}
int make2(){
if(board[5] == 2) return 5;
if(board[2] == 2) return 2;
if(board[4] == 2) return 4;
if(board[6] == 2) return 6;
if(board[8] == 2) return 8;
return 0;
}
int make4(){
if(board[1] == 2) return 1;
if(board[3] == 2) return 3;
if(board[7] == 2) return 7;
if(board[9] == 2) return 9;
return 0;
}
int posswin(int p){
// p==1 then X p==0 then O
int check_val, pos;
if(p == 1) check_val = 18;
else check_val = 50;
int i=1;
while(i<=9){//row check
if(board[i] * board[i+1] * board[i+2] == check_val){
if(board[i] == 2) return i;
if(board[i+1] == 2) return i+1;
if(board[i+2] == 2) return i+2;
}
i+=3;
}
i = 1;
while(i<=3){//column check
if(board[i] * board[i+3] * board[i+6] == check_val){
if(board[i] == 2) return i;
if(board[i+3] == 2) return i+3;
if(board[i+6] == 2) return i+6;
}
i++;
}
if(board[1] * board[5] * board[9] == check_val){
if(board[1] == 2) return 1;
if(board[5] == 2) return 5;
if(board[9] == 2) return 9;
}
if(board[3] * board[5] * board[7] == check_val){
if(board[3] == 2) return 3;
if(board[5] == 2) return 5;
if(board[7] == 2) return 7;
}
return 0;
}
void go(int n){
if((turn % 2)>0) board[n] = 3;
else board[n] = 5;
turn++;
}
void player_first(){
int pos;
ce.value="your turn";
check_draw();
draw_board();
if(input_changed==1){
input_changed=0;
pos = input_value;
if(board[pos] != 2)
player_first();
if(pos == posswin(player)){
go(pos);
draw_board();
ce.value = "player wins";
player_winXO=0;
return;
}
go(pos);
draw_board();
start_game();
}
}
void start_game(){
// p==1 then X p==0 then O
if(posswin(comp)>0){
go(posswin(comp));
flag = 1;
}
else if(posswin(player)>0) go(posswin(player));
else if(make2()>0) go(make2());
else go(make4());
draw_board();
if(flag>0){
ce.value="computer wins";
player_winXO=1;
}
else
player_first();
}
void check_draw(){
if(turn > 9){
ce.value = "to draw";
player_winXO=2;
}
}
void draw_board(){
for(int j=1;j<10;j++){
if(board[j] == 3) put_X_O(1,j); // X
else if(board[j] == 5) put_X_O(0,j); // O
}
DrawVectorLine(WHITE, 0.00, 0.33, 1.00, 0.33);
DrawVectorLine(WHITE, 0.00, 0.67, 1.00, 0.67);
DrawVectorLine(WHITE, 0.33, 0.00, 0.33, 1.00);
DrawVectorLine(WHITE, 0.67, 0.00, 0.67, 1.00);
}
void put_X_O(int XO,int pos){
// OX==1 then X, XO==0 then O
float x=0.,y=0.;
switch(pos){
case 0:break;
case 1:x=0.00;y=0.00;break;
case 2:x=0.33;y=0.00;break;
case 3:x=0.67;y=0.00;break;
case 4:x=0.00;y=0.33;break;
case 5:x=0.33;y=0.33;break;
case 6:x=0.67;y=0.33;break;
case 7:x=0.00;y=0.67;break;
case 8:x=0.33;y=0.67;break;
case 9:x=0.67;y=0.67;break;
}
if(XO==1) DrawVectorCross(Cross, x, y, 0.32,0.32); // X
else DrawVectorCircle(Oooo, x, y, 0.32,0.32); // O
}