OX / Mbed 2 deprecated 3DArray22

Dependencies:   mbed

Fork of 3DArray2 by OX

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 int map[5][5][5],rmap[5][5][5];
00004 int i , j , k , player , button , turn; 
00005 int check1[5][5][5] , check2[5][5][5];
00006 
00007 void Check_case1(){                 //คนแรกชนะ
00008     printf("Player 1 WIN!");
00009 }
00010 
00011 void Check_case2(){                 //คน2ชนะ
00012     printf("Player 2 WIN!");
00013     }
00014 
00015 void Check_error(){                 //เช็คคนแพ้
00016     if(player == 1){
00017         printf("Player 1 LOSE!");
00018         }
00019     else{
00020         printf("Player 2 LOSE!");
00021         }
00022     }
00023 
00024 void Check_map(){                   //แสดงmap
00025     for(i=0 ; i<5 ; i++){
00026         for(j=0 ; j<5 ; j++){ 
00027             for(k=0 ; k<5 ; k++){
00028                 if(map[i][j][k] == 5){
00029                     check1[i][j][k] = 1;
00030                     }
00031                 else if(map[i][j][k] == 6){
00032                     check2[i][j][k] = 1;
00033                     }
00034                 else{
00035                     check1[i][j][k] = 0;
00036                     check2[i][j][k] = 0;
00037                     }
00038                 } 
00039             }
00040         }
00041     }
00042     
00043 int main(){
00044     while(1){
00045         
00046         switch(player){                             // เลือกตำแหน่งคนลง
00047             
00048             case '1':
00049             i-- , j-- , k--;
00050                 if(map[i][j][k] == 0){
00051                     map[i][j][k] = 5;               // 5 = 'x'
00052                     turn++;
00053                     }
00054                 else{
00055                     Check_error();                  // ลงไมไ่ด้
00056                     }
00057             case '2':
00058                 i-- , j-- , k--;
00059                 if(map[i][j][k] == 0){
00060                     map[i][j][k] = 6;               // 6 = 'o'
00061                     turn++;
00062                     }
00063                 else{
00064                     Check_error();                  // ลงไมไ่ด้
00065                     }
00066             }
00067     
00068         if(i == 5 or j == 5 or k == 5){             // ลงเกิน map
00069             Check_error();
00070             }
00071         
00072         for(i=0 ; i<5 ; i++){          //แนว k
00073             for(j=0 ; j<5 ; j++){
00074                 for(k=0 ; k<3 ; k++){
00075                     if(map[i][j][k] and map[i][j][k+1] and map[i][j][k+2] == 5){
00076                         Check_case1();
00077                             }
00078                     else if(map[i][j][k] and map[i][j][k+1] and map[i][j][k+2] == 6){
00079                         Check_case2();
00080                             }
00081                         }
00082                     }
00083                 }
00084         
00085         for(i=0 ; i<5 ; i++){           // แนว j
00086             for(k=0 ; k<5 ; k++){
00087                 for(j=0 ; j<3 ; j++){   
00088                     if(map[i][j][k] and map[i][j+1][k] and map[i][j+2][k] == 5){
00089                         Check_case1();
00090                             }
00091                     else if(map[i][j][k] and map[i][j+1][k] and map[i][j+2][k] == 6){
00092                         Check_case2();
00093                             }
00094                         }
00095                     }
00096                 }  
00097         
00098         for(j=0 ; j<5 ; j++){           // แนว i
00099             for(k=0 ; k<5 ; k++){
00100                 for(i=0 ; i<3 ; i++){
00101                     if(map[i][j][k] and map[i+1][j][k] and map[i+2][j][k] == 5){
00102                         Check_case1();
00103                             }
00104                     else if(map[i][j][k] and map[i+1][j][k] and map[i+2][j][k] == 6){
00105                         Check_case2();
00106                             }
00107                         }
00108                     }
00109                 } 
00110                 
00111         for(i=0 ; i<5 ; i++){       // แนวทแยง i เป็นฐานขึ้น
00112             for(j=0 ; j<3 ; j++){
00113                 for(k=0 ; k<3 ; k++){
00114                     if(map[i][j][k] and map[i][j+1][k+1] and map[i][j+2][k+2] == 5){
00115                          Check_case1();
00116                         }
00117                     else if(map[i][j][k] and map[i][j+1][k+1] and map[i][j+2][k+2] == 6){
00118                          Check_case2();
00119                         }
00120                     }
00121                 }
00122             }
00123             
00124         for(j=0 ; j<5 ; j++){       // แนวทแยง j เป็นฐานขึ้น
00125             for(i=0 ; i<3 ; i++){
00126                 for(k=0 ; k<3 ; k++){
00127                     if(map[i][j][k] and map[i+1][j][k+1] and map[i+2][j][k+2] == 5){
00128                          Check_case1();
00129                         }
00130                     else if(map[i][j][k] and map[i+1][j][k+1] and map[i+2][j][k+2] == 6){
00131                          Check_case2();
00132                         }
00133                     }
00134                 }
00135             }
00136             
00137         for(k=0 ; k<5 ; k++){       // แนวทแยง k เป็นฐานขึ้น
00138             for(i=0 ; i<3 ; i++){
00139                 for(j=0 ; j<3 ; j++){
00140                     if(map[i][j][k] and map[i+1][j+1][k] and map[i+2][j+2][k] == 5){
00141                         Check_case1();
00142                         }
00143                     else if(map[i][j][k] and map[i+1][j+1][k] and map[i+2][j+2][k] == 6){
00144                          Check_case2();
00145                         }
00146                     }
00147                 }
00148             }
00149             
00150         for(i=0 ; i<5 ; i++){       // แนวทแยง i เป็นฐานลง
00151             for(j=0 ; j<3 ; j++){
00152                 for(k=4 ; k>1 ; k--){
00153                     if(map[i][j][k] and map[i][j+1][k-1] and map[i][j+2][k-2] == 5){
00154                          Check_case1();
00155                         }
00156                     else if(map[i][j][k] and map[i][j+1][k-1] and map[i][j+2][k-2] == 6){
00157                          Check_case2();
00158                         }
00159                     }
00160                 }
00161             }
00162             
00163         for(j=0 ; j<5 ; j++){       // แนวทแยง j เป็นฐานลง
00164             for(i=0 ; i<3 ; i++){
00165                 for(k=4 ; k>1 ; k--){
00166                     if(map[i][j][k] and map[i+1][j][k-1] and map[i+2][j][k-2] == 5){
00167                          Check_case1();
00168                         }
00169                     else if(map[i][j][k] and map[i+1][j][k-1] and map[i+2][j][k-2] == 6){
00170                          Check_case2();
00171                         }
00172                     }
00173                 }
00174             }
00175             
00176          for(k=0 ; k<5 ; k++){      // แนวทแยง k เป็นฐานลง
00177             for(i=0 ; i<3 ; i++){
00178                 for(j=4 ; j>1 ; j--){
00179                     if(map[i][j][k] and map[i+1][j-1][k] and map[i+2][j-2][k] == 5){
00180                          Check_case1();
00181                         }
00182                     else if(map[i][j][k] and map[i+1][j-1][k] and map[i+2][j-2][k] == 6){
00183                          Check_case2();
00184                         }
00185                     }
00186                 }
00187             }
00188         for(i=0 ; i<3 ; i++){       // แนวทแยงแบบตัด1
00189             for(j=0 ; j<3 ; j++){
00190                for(k=0 ; k<3 ; k++){
00191                    if(map[i][j][k] and map[i+1][j+1][k+1] and map[i+2][j+2][k+2] == 5){
00192                         Check_case1();
00193                        }   
00194                     else if(map[i][j][k] and map[i+1][j+1][k+1] and map[i+2][j+2][k+2] == 6){
00195                         Check_case2();
00196                         }
00197                     }
00198                 }
00199             }
00200         for(i=4 ; i>1 ; i--){       // แนวทแยงแบบตัด2
00201             for(j=0 ; j<3 ; j++){
00202                for(k=0 ; k<3 ; k++){
00203                    if(map[i][j][k] and map[i-1][j+1][k+1] and map[i-2][j+2][k+2] == 5){
00204                         Check_case1();
00205                        }   
00206                     else if(map[i][j][k] and map[i-1][j+1][k+1] and map[i-2][j+2][k+2] == 6){
00207                         Check_case2();
00208                         }
00209                     }
00210                 }
00211             }
00212         for(i=4 ; i>1 ; i--){       // แนวทแยงแบบตัด 3
00213             for(j=4 ; j>1 ; j--){
00214                for(k=0 ; k<3 ; k++){
00215                    if(map[i][j][k] and map[i-1][j-1][k+1] and map[i-2][j-2][k+2] == 5){
00216                         Check_case1();
00217                        }   
00218                     else if(map[i][j][k] and map[i-1][j-1][k+1] and map[i-2][j-2][k+2] == 6){
00219                         Check_case2();
00220                         }
00221                     }
00222                 }
00223             }
00224         for(i=0 ; i<3 ; i++){       // แนวทแยงแบบตัด4
00225             for(j=4 ; j>1 ; j--){
00226                for(k=0 ; k<3 ; k++){
00227                    if(map[i][j][k] and map[i+1][j-1][k+1] and map[i+2][j-2][k+2] == 5){
00228                         Check_case1();
00229                        }   
00230                     else if(map[i][j][k] and map[i+1][j-1][k+1] and map[i+2][j-2][k+2] == 6){
00231                         Check_case2();
00232                         }
00233                     }
00234                 }
00235             }
00236                
00237                 
00238             }
00239             
00240         }