Othello
Dependencies: mbed
Revision 0:8c7f841516ab, committed 2018-09-04
- Comitter:
- Yuto_K
- Date:
- Tue Sep 04 04:07:41 2018 +0000
- Commit message:
- Othello;
Changed in this revision
diff -r 000000000000 -r 8c7f841516ab color.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/color.h Tue Sep 04 04:07:41 2018 +0000 @@ -0,0 +1,10 @@ +#define MAX 255 +#define MIN 0 +#define CCLEAR MIN,MIN,MIN +#define CRED MAX,MIN,MIN +#define CGREEN MIN,MAX,MIN +#define CBLUE MIN,MIN,MAX +#define CYELLOW MAX,MAX,MIN +#define CCYAN MIN,MAX,MAX +#define CMAGENTA MAX,MIN,MAX +#define CWHITE MAX,MAX,MAX \ No newline at end of file
diff -r 000000000000 -r 8c7f841516ab led_func.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/led_func.h Tue Sep 04 04:07:41 2018 +0000 @@ -0,0 +1,82 @@ +#include "necleo_pin.h" +#include "color.h" + +#define L 0 +#define H 1 + +#define R 0 +#define G 1 +#define B 2 + +class Dot{ + public: + char red; + char green; + char blue; + void SetColor(char r,char g,char b){ + red=r; + green=g; + blue=b; + } +}; + +Dot led_board[16][16]; + +void ResetLEDBoard(){ + for(char i=0;i<16;i++){ + for(char j=0;j<16;j++){ + led_board[i][j].SetColor(CCLEAR); + } + } +} + +void PrintLEDBoard(){ + static const char list[4]={0,2,1,3}; + static char count=0; + CLK=L; + LAT=L; + for(char i=0;i<4;i++){ + OE=H; + Address=list[i]; + for(char j=0;j<32;j++){ + if(j<8){ + Red1=led_board[i][7-j].red>count; + Green1=led_board[i][7-j].green>count; + Blue1=led_board[i][7-j].blue>count; + Red2=led_board[i+8][7-j].red>count; + Green2=led_board[i+8][7-j].green>count; + Blue2=led_board[i+8][7-j].blue>count; + }else if(j<16){ + Red1=led_board[i+4][j-8].red>count; + Green1=led_board[i+4][j-8].green>count; + Blue1=led_board[i+4][j-8].blue>count; + Red2=led_board[i+12][j-8].red>count; + Green2=led_board[i+12][j-8].green>count; + Blue2=led_board[i+12][j-8].blue>count; + }else if(j<24){ + Red1=led_board[i][31-j].red>count; + Green1=led_board[i][31-j].green>count; + Blue1=led_board[i][31-j].blue>count; + Red2=led_board[i+8][31-j].red>count; + Green2=led_board[i+8][31-j].green>count; + Blue2=led_board[i+8][31-j].blue>count; + }else{ + Red1=led_board[i+4][j-16].red>count; + Green1=led_board[i+4][j-16].green>count; + Blue1=led_board[i+4][j-16].blue>count; + Red2=led_board[i+12][j-16].red>count; + Green2=led_board[i+12][j-16].green>count; + Blue2=led_board[i+12][j-16].blue>count; + } + CLK=H; + CLK=L; + } + LAT=H; + LAT=L; + OE=L; + wait_us(1); + } + OE=H; + count++; + if(count>100)count=0; +} \ No newline at end of file
diff -r 000000000000 -r 8c7f841516ab main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Sep 04 04:07:41 2018 +0000 @@ -0,0 +1,23 @@ +#include "led_func.h" +#include "othellomain.h" + +Ticker LedBoard; + +void SetLEDBoard(Board board){ + for(uint8_t i=0;i<8;i++){ + for(uint8_t j=0;j<8;j++){ + if(board.board[i][j]==BLACK)led_board[i][j].SetColor(CBLUE); + if(board.board[i][j]==WHITE)led_board[i][j].SetColor(CGREEN); + } + } +} + +int main(){ + ResetLEDBoard(); + RandomAI player1(BLACK); + RandomAI player2(WHITE); + Board mainboard; + LedBoard.attach_us(&PrintLEDBoard,100); + Play(player1,player2,mainboard); + while(1){} +} \ No newline at end of file
diff -r 000000000000 -r 8c7f841516ab mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Tue Sep 04 04:07:41 2018 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/a7c7b631e539 \ No newline at end of file
diff -r 000000000000 -r 8c7f841516ab necleo_pin.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/necleo_pin.h Tue Sep 04 04:07:41 2018 +0000 @@ -0,0 +1,12 @@ +#include "mbed.h" +DigitalOut Red1(D0); +DigitalOut Green1(A5); +DigitalOut Blue1(D1); +DigitalOut Red2(D8); +DigitalOut Green2(A3); +DigitalOut Blue2(D3); +BusOut Address(A2,D4); +DigitalOut CLK(D6); +DigitalOut LAT(A0); +DigitalOut OE(D7); +DigitalOut myLED(LED1); \ No newline at end of file
diff -r 000000000000 -r 8c7f841516ab othello.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/othello.h Tue Sep 04 04:07:41 2018 +0000 @@ -0,0 +1,232 @@ +#include <stdint.h> +#include <stdlib.h> +#include <string.h> +#include <vector> +#define EMPTY 0 +#define BLACK 1 +#define WHITE -1 + +class Board{ + public: + int8_t board[8][8]; + int8_t turn; + Board(){ + for(int8_t i=0;i<8;i++){ + for(int8_t j=0;j<8;j++)board[i][j]=EMPTY; + } + board[3][4]=BLACK;board[4][3]=BLACK; + board[3][3]=WHITE;board[4][4]=WHITE; + turn=BLACK; + } + void SetBoard(int8_t board[8][8]){ + memcpy(this->board,board,sizeof board); + } +}; + +void SetLEDBoard(Board board); + +/*void ResetBoard(){ + for(uint8_t i=0;i<8;i++){ + for(uint8_t j=0;j<8;j++)board[i][j]=EMPTY; + } + board[3][4]=BLACK;board[4][3]=BLACK; + board[3][3]=WHITE;board[4][4]=WHITE; +}*/ + +bool CanRight(Board board,int8_t x,int8_t y,int8_t color){ + int8_t enemy=-color; + if(x<=5&&board.board[x+1][y]==enemy){ + for(int8_t i=x+2;i<8;i++){ + if(board.board[i][y]==color)return true; + else if(board.board[i][y]==EMPTY)break; + } + } + return false; +} + +bool CanLeft(Board board,int8_t x,int8_t y,int8_t color){ + int8_t enemy=-color; + if(x>=2&&board.board[x-1][y]==enemy){ + for(int8_t i=x-2;i>=0;i--){ + if(board.board[i][y]==color)return true; + else if(board.board[i][y]==EMPTY)break; + } + } + return false; +} + +bool CanUp(Board board,int8_t x,int8_t y,int8_t color){ + int8_t enemy=-color; + if(y>=2&&board.board[x][y-1]==enemy){ + for(int8_t i=y-2;i>=0;i--){ + if(board.board[x][i]==color)return true; + else if(board.board[x][i]==EMPTY)break; + } + } + return false; +} + +bool CanDown(Board board,int8_t x,int8_t y,int8_t color){ + int8_t enemy=-color; + if(y<=5&&board.board[x][y+1]==enemy){ + for(int8_t i=y+2;i<8;i++){ + if(board.board[x][i]==color)return true; + else if(board.board[x][i]==EMPTY)break; + } + } + return false; +} + +bool CanRightUp(Board board,int8_t x,int8_t y,int8_t color){ + int8_t enemy=-color; + if(x<=5&&y>=2&&board.board[x+1][y-1]==enemy){ + int8_t i=2; + while(x+i<8&&y-i>=0){ + if(board.board[x+i][y-i]==color)return true; + else if(board.board[x+i][y-i]==EMPTY)break; + i++; + } + } + return false; +} + +bool CanRightDown(Board board,int8_t x,int8_t y,int8_t color){ + int8_t enemy=-color; + if(x<=5&&y<=5&&board.board[x+1][y+1]==enemy){ + int8_t i=2; + while(x+i<8&&y+i<8){ + if(board.board[x+i][y+i]==color)return true; + else if(board.board[x+i][y+i]==EMPTY)break; + i++; + } + } + return false; +} + +bool CanLeftDown(Board board,int8_t x,int8_t y,int8_t color){ + int8_t enemy=-color; + if(x>=2&&y<=5&&board.board[x-1][y+1]==enemy){ + int8_t i=2; + while(x-i>=0&&y+i<8){ + if(board.board[x-i][y+i]==color)return true; + else if(board.board[x-i][y+i]==EMPTY)break; + i++; + } + } + return false; +} + +bool CanLeftUp(Board board,int8_t x,int8_t y,int8_t color){ + int8_t enemy=-color; + if(x>=2&&y>=2&&board.board[x-1][y-1]==enemy){ + int8_t i=2; + while(x-i>=0&&y-i>=0){ + if(board.board[x-i][y-i]==color)return true; + else if(board.board[x-i][y-i]==EMPTY)break; + i++; + } + } + return false; +} + +bool CanPut(Board board,int8_t x,int8_t y,int8_t color){ + return CanRight(board,x,y,color)||CanLeft(board,x,y,color)||CanUp(board,x,y,color)||CanDown(board,x,y,color)||CanRightUp(board,x,y,color)||CanRightDown(board,x,y,color)||CanLeftDown(board,x,y,color)||CanLeftUp(board,x,y,color); +} + +std::vector<uint8_t> GetPutCoords(Board board,int8_t color){ + std::vector<uint8_t> coords; + for(int8_t i=0;i<8;i++){ + for(int8_t j=0;j<8;j++){ + led_board[i+8][j+8].red=255; + if(CanPut(board,i,j,color))coords.push_back(i*8+j);//ここの18回目の実行で上のCanDownからCanRightUpの間で止まる + } + } + return coords; +} + +Board PutStone(Board board,int8_t x,int8_t y,int8_t color){ + Board _dummyboard; + int8_t dummyboard[8][8]; + int8_t enemy=-color; + uint8_t i; + memcpy(dummyboard,board.board,sizeof board.board); + _dummyboard.SetBoard(dummyboard); + if(CanRight(_dummyboard,x,y,color)){ + i=x+1; + while(dummyboard[i][y]==enemy){ + dummyboard[i][y]=color; + i+=1; + } + } + if(CanLeft(_dummyboard,x,y,color)){ + i=x-1; + while(dummyboard[i][y]==enemy){ + dummyboard[i][y]=color; + i-=1; + } + } + if(CanUp(_dummyboard,x,y,color)){ + i=y-1; + while(dummyboard[x][i]==enemy){ + dummyboard[x][i]=color; + i-=1; + } + } + if(CanDown(_dummyboard,x,y,color)){ + i=y+1; + while(dummyboard[x][i]==enemy){ + dummyboard[x][i]=color; + i+=1; + } + } + if(CanRightUp(_dummyboard,x,y,color)){ + i=1; + while(dummyboard[x+i][y-i]==enemy){ + dummyboard[x+i][y-i]=color; + i+=1; + } + } + if(CanRightDown(_dummyboard,x,y,color)){ + i=1; + while(dummyboard[x+i][y+i]==enemy){ + dummyboard[x+i][y+i]=color; + i+=1; + } + } + if(CanLeftDown(_dummyboard,x,y,color)){ + i=1; + while(dummyboard[x-i][y+i]==enemy){ + dummyboard[x-i][y+i]=color; + i+=1; + } + } + if(CanLeftUp(_dummyboard,x,y,color)){ + i=1; + while(dummyboard[x-i][y-i]==enemy){ + dummyboard[x-i][y-i]=color; + i+=1; + } + } + dummyboard[x][y]=color; + _dummyboard.SetBoard(dummyboard); + return _dummyboard; +} + +uint8_t GetScore(Board board,int8_t color){ + uint8_t score=0; + for(int8_t i=0;i<8;i++){ + for(int8_t j=0;j<8;j++){ + if(board.board[i][j]==color)score++; + } + } + return score; +} + + +bool GameSet(Board board){ + led_board[8][0].red=255; + std::vector<uint8_t> black(GetPutCoords(board,BLACK)); + std::vector<uint8_t> white(GetPutCoords(board,WHITE)); + led_board[0][8].red=255; + return black.size()==0&&white.size()==0; +} \ No newline at end of file
diff -r 000000000000 -r 8c7f841516ab othellomain.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/othellomain.h Tue Sep 04 04:07:41 2018 +0000 @@ -0,0 +1,25 @@ +#include "player.h" + +void Play(RandomAI player1,RandomAI player2,Board board){ + uint8_t put_move; + while(1){ + SetLEDBoard(board); + if(GameSet(board))break; + led_board[15][14].red=255; + if(board.turn==player1.color){ + led_board[14][14].red=255; + put_move=player1.PutMove(board); + board.SetBoard(PutStone(board,put_move/8,put_move%8,player1.color).board); + if(GetPutCoords(board,player2.color).size()!=0)board.turn=-board.turn; + } + SetLEDBoard(board); + if(GameSet(board))break; + if(board.turn==player2.color){ + led_board[13][13].red=255; + put_move=player2.PutMove(board); + board.SetBoard(PutStone(board,put_move/8,put_move%8,player2.color).board); + if(GetPutCoords(board,player1.color).size()!=0)board.turn=-board.turn; + } + } + led_board[15][15].red=255; +} \ No newline at end of file
diff -r 000000000000 -r 8c7f841516ab player.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/player.h Tue Sep 04 04:07:41 2018 +0000 @@ -0,0 +1,26 @@ +#include "othello.h" + +/*class Player{ + public: + int8_t color; + Player(int8_t color){ + this->color=color; + } + + uint8_t PutMove(int8_t board[8][8]){ + return 0; + } +};*/ + +class RandomAI{ + public: + int8_t color; + RandomAI(int8_t _color){ + this->color=_color; + } + uint8_t PutMove(Board board){ + std::vector<uint8_t> coords(GetPutCoords(board,this->color)); + uint8_t idx=rand()%coords.size(); + return coords[idx]; + } +}; \ No newline at end of file