OX V1

Dependencies:   mbed

Moop.h

Committer:
arthicha
Date:
2016-12-05
Revision:
2:84a84429750a
Parent:
0:6182212860fb

File content as of revision 2:84a84429750a:

#include "mbed.h"
 
 
 
class Moop
{
    private:
        int A , B , C , player , button , turn; 
        int WMbox[6];
        
        Moop::Moop()
        {
            WMbox[0] = 3;
            WMbox[1] = 3;
            WMbox[2] = 3;
            WMbox[3] = -1;
            WMbox[4] = -1;
            WMbox[5] = -1; 
        }
        
        void Moop::Check_case1(){                 //คนแรกชนะ
            printf("Player 1 WIN!");
        }
         
        void Moop::Check_case2(){                 //คน2ชนะ
            printf("Player 2 WIN!");
        }
         
        void Moop::Check_error(){                 //เช็คคนแพ้
            if(this->player == 1){
                printf("Player 1 LOSE!");
            }
            else{
                printf("Player 2 LOSE!");
            }
        }
        
        int Moop::Cross(int degree)
        {
            if (degree == 0)
            {
                return 1;
            }else if (degree == 180)
            {
                return -1;
            }else
            {
                return 0;   
            }
        }
    
        int Moop::Zine(int degree)
        {
            if (degree == 90)
            {
                return 1;
            }else if (degree == -90)
            {
                return -1;
            }else
            {
                return 0;   
            }
        }
    public:
        int map[5][5][5],rmap[5][5][5]; //store map.
        int Check1[5][5][5] , Check2[5][5][5]; //convert to x or o map.
        
        
        
        
        int  Moop::GetCoordinateX()
        {
            if (this->WMbox[0] != -1)
            {
              return this->WMbox[0];   
            }else
            {
                return this->WMbox[3];   
            }
        }
        
        int Moop::GetCoordinateY()
        {
            if (this->WMbox[1] != -1)
            {
              return this->WMbox[1];   
            }else
            {
                return this->WMbox[4];   
            }
        }
        
        int  Moop::GetCoordinateZ()
        {
            if (this->WMbox[2] != -1)
            {
              return this->WMbox[2];   
            }else
            {
                return this->WMbox[5];   
            }
        }
        
        void Moop::ChangeOX(int xx, int yy,int zz,int lx,int ly,int lz)
        {
            if (xx != -1)
            {
                this->WMbox[0] = xx;
                this->WMbox[1] = yy;
                this->WMbox[2] = zz;
                this->WMbox[3] = lx;
                this->WMbox[4] = ly;
                this->WMbox[5] = lz;   
            }else
            {
                this->WMbox[0] = lx;
                this->WMbox[1] = ly;
                this->WMbox[2] = lz;
                this->WMbox[3] = xx;
                this->WMbox[4] = yy;
                this->WMbox[5] = zz;
            }
        }
        
        void Moop::Choose(int box)
        {
            if (box)
            {
                this->WMbox[0] = -1;
                this->WMbox[1] = -1;
                this->WMbox[2] = -1;   
            }else
            {
                this->WMbox[3] = -1;
                this->WMbox[4] = -1;
                this->WMbox[5] = -1;   
            }
        }
        
        void Rotate(int role,int pitch,int yaw)
        {
            if ((pitch == 90) || (pitch == -90)) //{{1,0,0},{0,cosy,-siny},{0,siny,cosy}}{{cosx,0,+sinx},{0,1,0},{-sinx,0,cosx}}
            {
                for (int i=0;i<5;i++)
                {
                    for (int j=0;j<5;j++)
                    {
                        for(int k=0;k<5;k++)
                        {
                            this->A =  ((i-2)*this->Cross(pitch))+((k-2)*this->Zine(pitch));
                            this->B =  ((j-2)*this->Cross(yaw))+((i-2)*this->Zine(pitch)*this->Zine(yaw))-((k-2)*this->Zine(yaw)*this->Cross(pitch));
                            this->C =  ((j-2)*this->Zine(yaw))-((i-2)*this->Cross(yaw)*this->Zine(pitch))+((k-2)*this->Cross(yaw)*this->Cross(pitch));
                            this->rmap[i+2][j+2][k+2]=this->map[i][j][k];
                        }   
                    }   
                }
            }else if ((role == 90) || (role == -90)) // {{cosy,0,+siny},{0,1,0},{-siny,0,cosy}}{{1,0,0},{0,cosx,-sinx},{0,sinx,cosx}}
            {
                for (int i=0;i<5;i++)
                {
                    for (int j=0;j<5;j++)
                    {
                        for(int k=0;k<5;k++)
                        {
                            this->A =  ((i-2)*this->Cross(yaw))   + ((j-2)*this->Zine(role)*this->Zine(yaw)) +((k-2)*this->Zine(yaw)*this->Cross(role));
                            this->B =  ((j-2)*this->Cross(role))-((k-2)*this->Zine(role));
                            this->C =  -((i-2)*this->Zine(yaw))+((j-2)*this->Cross(yaw)*this->Zine(role))+((k-2)*this->Cross(yaw)*this->Cross(role));
                            this->rmap[i+2][j+2][k+2]=this->map[i][j][k];
                        }   
                    }   
                } 
            }else
            {
                for (int i=0;i<5;i++)
                {
                    for (int j=0;j<5;j++)
                    {
                        for(int k=0;k<5;k++)
                        {
                            this->A =  ((i-2)*this->Cross(yaw))-((j-2)*this->Zine(yaw));
                            this->B =  ((i-2)*this->Zine(yaw))+((j-2)*this->Cross(yaw));
                            this->C =  (k-2);
                            this->rmap[i+2][j+2][k+2]=this->map[i][j][k];
                        }   
                    }   
                } 
            }
            for (int i=0;i<5;i++)
                {
                    for (int j=0;j<5;j++)
                    {
                        for(int k=0;k<5;k++)
                        {
                            this->map[i][j][k]=this->rmap[i][j][k];
                        }   
                    }   
                }
        }
        
        int Moop::Update(int i,int j,int k,int turner){
        if(1){
            
            switch(turner%2){                             // เลือกตำแหน่งคนลง
                
                case '0':
                i-- , j-- , k--;
                    if(this->map[i][j][k] == 0){
                        this->map[i][j][k] = 5;               // 5 = 'x'
                        this->turn++;
                        }
                    else{
                        this->Check_error();                  // ลงไมไ่ด้
                        }
                case '1':
                    i-- , j-- , k--;
                    if(this->map[i][j][k] == 0){
                        this->map[i][j][k] = 6;               // 6 = 'o'
                        this->turn++;
                        }
                    else{
                        this->Check_error();                  // ลงไมไ่ด้
                        }
                }
            if(i == 5 or j == 5 or k == 5){             // ลงเกิน map
                this->Check_error();
                }
            
            for(i=0 ; i<5 ; i++){          //แนว k
                for(j=0 ; j<5 ; j++){
                    for(k=0 ; k<3 ; k++){
                        if(this->map[i][j][k] and this->map[i][j][k+1] and this->map[i][j][k+2] == 5){
                            this->Check_case1();
                                }
                        else if(this->map[i][j][k] and this->map[i][j][k+1] and this->map[i][j][k+2] == 6){
                            this->Check_case2();
                                }
                            }
                        }
                    }
            
            for(i=0 ; i<5 ; i++){           // แนว j
                for(k=0 ; k<5 ; k++){
                    for(j=0 ; j<3 ; j++){   
                        if(this->map[i][j][k] and this->map[i][j+1][k] and this->map[i][j+2][k] == 5){
                            this->Check_case1();
                                }
                        else if(this->map[i][j][k] and this->map[i][j+1][k] and this->map[i][j+2][k] == 6){
                            this->Check_case2();
                                }
                            }
                        }
                    }  
            
            for(j=0 ; j<5 ; j++){           // แนว i
                for(k=0 ; k<5 ; k++){
                    for(i=0 ; i<3 ; i++){
                        if(this->map[i][j][k] and this->map[i+1][j][k] and this->map[i+2][j][k] == 5){
                            this->Check_case1();
                                }
                        else if(this->map[i][j][k] and this->map[i+1][j][k] and this->map[i+2][j][k] == 6){
                            this->Check_case2();
                                }
                            }
                        }
                    } 
                    
            for(i=0 ; i<5 ; i++){       // แนวทแยง i เป็นฐานขึ้น
                for(j=0 ; j<3 ; j++){
                    for(k=0 ; k<3 ; k++){
                        if(this->map[i][j][k] and this->map[i][j+1][k+1] and this->map[i][j+2][k+2] == 5){
                             this->Check_case1();
                            }
                        else if(this->map[i][j][k] and this->map[i][j+1][k+1] and this->map[i][j+2][k+2] == 6){
                             this->Check_case2();
                            }
                        }
                    }
                }
                
            for(j=0 ; j<5 ; j++){       // แนวทแยง j เป็นฐานขึ้น
                for(i=0 ; i<3 ; i++){
                    for(k=0 ; k<3 ; k++){
                        if(this->map[i][j][k] and this->map[i+1][j][k+1] and this->map[i+2][j][k+2] == 5){
                             this->Check_case1();
                            }
                        else if(this->map[i][j][k] and this->map[i+1][j][k+1] and this->map[i+2][j][k+2] == 6){
                             this->Check_case2();
                            }
                        }
                    }
                }
                
            for(k=0 ; k<5 ; k++){       // แนวทแยง k เป็นฐานขึ้น
                for(i=0 ; i<3 ; i++){
                    for(j=0 ; j<3 ; j++){
                        if(this->map[i][j][k] and this->map[i+1][j+1][k] and this->map[i+2][j+2][k] == 5){
                            this->Check_case1();
                            }
                        else if(this->map[i][j][k] and this->map[i+1][j+1][k] and this->map[i+2][j+2][k] == 6){
                             this->Check_case2();
                            }
                        }
                    }
                }
                
            for(i=0 ; i<5 ; i++){       // แนวทแยง i เป็นฐานลง
                for(j=0 ; j<3 ; j++){
                    for(k=4 ; k>1 ; k--){
                        if(this->map[i][j][k] and this->map[i][j+1][k-1] and this->map[i][j+2][k-2] == 5){
                             this->Check_case1();
                            }
                        else if(this->map[i][j][k] and this->map[i][j+1][k-1] and this->map[i][j+2][k-2] == 6){
                             this->Check_case2();
                            }
                        }
                    }
                }
                
            for(j=0 ; j<5 ; j++){       // แนวทแยง j เป็นฐานลง
                for(i=0 ; i<3 ; i++){
                    for(k=4 ; k>1 ; k--){
                        if(this->map[i][j][k] and this->map[i+1][j][k-1] and this->map[i+2][j][k-2] == 5){
                             this->Check_case1();
                            }
                        else if(this->map[i][j][k] and this->map[i+1][j][k-1] and this->map[i+2][j][k-2] == 6){
                             this->Check_case2();
                            }
                        }
                    }
                }
                
             for(k=0 ; k<5 ; k++){      // แนวทแยง k เป็นฐานลง
                for(i=0 ; i<3 ; i++){
                    for(j=4 ; j>1 ; j--){
                        if(this->map[i][j][k] and this->map[i+1][j-1][k] and this->map[i+2][j-2][k] == 5){
                             this->Check_case1();
                            }
                        else if(this->map[i][j][k] and this->map[i+1][j-1][k] and this->map[i+2][j-2][k] == 6){
                             this->Check_case2();
                            }
                        }
                    }
                }
            for(i=0 ; i<3 ; i++){       // แนวทแยงแบบตัด1
                for(j=0 ; j<3 ; j++){
                   for(k=0 ; k<3 ; k++){
                       if(this->map[i][j][k] and this->map[i+1][j+1][k+1] and this->map[i+2][j+2][k+2] == 5){
                            this->Check_case1();
                           }   
                        else if(this->map[i][j][k] and this->map[i+1][j+1][k+1] and this->map[i+2][j+2][k+2] == 6){
                            this->Check_case2();
                            }
                        }
                    }
                }
            for(i=4 ; i>1 ; i--){       // แนวทแยงแบบตัด2
                for(j=0 ; j<3 ; j++){
                   for(k=0 ; k<3 ; k++){
                       if(this->map[i][j][k] and this->map[i-1][j+1][k+1] and this->map[i-2][j+2][k+2] == 5){
                            this->Check_case1();
                           }   
                        else if(this->map[i][j][k] and this->map[i-1][j+1][k+1] and this->map[i-2][j+2][k+2] == 6){
                            this->Check_case2();
                            }
                        }
                    }
                }
            for(i=4 ; i>1 ; i--){       // แนวทแยงแบบตัด 3
                for(j=4 ; j>1 ; j--){
                   for(k=0 ; k<3 ; k++){
                       if(this->map[i][j][k] and this->map[i-1][j-1][k+1] and this->map[i-2][j-2][k+2] == 5){
                            this->Check_case1();
                           }   
                        else if(this->map[i][j][k] and this->map[i-1][j-1][k+1] and this->map[i-2][j-2][k+2] == 6){
                            this->Check_case2();
                            }
                        }
                    }
                }
            for(i=0 ; i<3 ; i++){       // แนวทแยงแบบตัด4
                for(j=4 ; j>1 ; j--){
                   for(k=0 ; k<3 ; k++){
                       if(this->map[i][j][k] and this->map[i+1][j-1][k+1] and this->map[i+2][j-2][k+2] == 5){
                            this->Check_case1();
                           }   
                        else if(this->map[i][j][k] and this->map[i+1][j-1][k+1] and this->map[i+2][j-2][k+2] == 6){
                            this->Check_case2();
                            }
                        }
                    }
                }
                   
                    
                }
                
            }
     
        
        
        void Moop::Check_map(){                   //แสดงthis->map
            for(this->A=0 ; this->A<5 ; this->A++){
                for(this->B=0 ; this->B<5 ; this->B++){ 
                    for(this->C=0 ; this->C<5 ; this->C++){
                        if(this->map[this->A][this->B][this->C] == 5){
                            this->Check1[this->A][this->B][this->C] = 1;
                            }
                        else if(this->map[this->A][this->B][this->C] == 6){
                            this->Check2[this->A][this->B][this->C] = 1;
                            }
                        else{
                            this->Check1[this->A][this->B][this->C] = 0;
                            this->Check2[this->A][this->B][this->C] = 0;
                            }
                        } 
                    }
                }
            }
}