Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #define size 3
- typedef struct move{
- int score;
- int x;
- int y;
- }moveEval;
- void printBoard(char board[size][size]){
- int i,j;
- printf("\n");
- printf(" 0 1 2\n");
- for(i=0;i<size;i++)
- for(j=0;j<size;j++){
- if(j == 0)
- printf("%d ",i);
- printf("|%c|",board[i][j]);
- if(j == 2)
- printf("\n");
- }
- }
- char checkWin(char board[size][size]){
- if(board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X' || board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O')
- return board[0][0];
- else if(board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] == 'X' || board[1][0] == 'O' && board[1][1] == 'O' && board[1][2] == 'O')
- return board[1][0];
- else if(board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X' || board[2][0] == 'O' && board[2][1] == 'O' && board[2][2] == 'O')
- return board[2][0];
- else if(board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X' || board[0][0] == 'O' && board[1][0] == 'O' && board[2][0] == 'O')
- return board[0][0];
- else if(board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X' || board[0][1] == 'O' && board[1][1] == 'O' && board[2][1] == 'O')
- return board[0][1];
- else if(board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X' || board[0][2] == 'O' && board[1][2] == 'O' && board[2][2] == 'O')
- return board[0][2];
- else if(board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X' || board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O')
- return board[0][0];
- else if(board[0][2] == 'X' && board[1][1] == 'X' && board[2][0] == 'X' || board[0][2] == 'O' && board[1][1] == 'O' && board[2][0] == 'O')
- return board[0][2];
- return ' ';
- }
- int checkBoard(char board[size][size]){
- int i,j;
- for(i = 0 ; i < size ; i++)
- for(j = 0 ; j < size ; j++)
- if(board[i][j] == ' ')
- return 1;
- return 0;
- }
- int checkScore(char board[size][size], int x , int y){
- int i,j;
- int northsouth = 0, eastwest = 0, northwestsoutheast = 0, northeastsouthwest = 0;
- //printBoard(board);
- for(i = x - 1, j = y ; i >= 0 ; i--){ // NORTH
- if(board[i][j] == board[x][y]){
- if(board[x][y] == 'X')
- northsouth += 3;
- else if(board[x][y] == 'O')
- northsouth -= 3;
- }else if(board[i][j] == ' '){
- if(board[x][y] == 'X')
- northsouth += 1;
- else if(board[x][y] == 'O')
- northsouth -= 1;
- }else{
- northsouth = 0;
- }
- }
- for(i = x + 1, j = y; i < size; i++){ // SOUTH
- if(board[i][j] == board[x][y]){
- if(board[x][y] == 'X')
- northsouth += 3;
- else if(board[x][y] == 'O')
- northsouth -= 3;
- }else if(board[i][j] == ' '){
- if(board[x][y] == 'X')
- northsouth += 1;
- else if(board[x][y] == 'O')
- northsouth -= 1;
- }else{
- northsouth = 0;
- }
- }
- for(j = y + 1, i = x; j < size; j++){ // EAST
- if(board[i][j] == board[x][y]){
- if(board[x][y] == 'X')
- eastwest += 3;
- else if(board[x][y] == 'O')
- eastwest -= 3;
- }else if(board[i][j] == ' '){
- if(board[x][y] == 'X')
- eastwest += 1;
- else if(board[x][y] == 'O')
- eastwest -= 1;
- }else{
- eastwest = 0;
- }
- }
- for(j = y - 1, i = x ; j >= 0 ; j--){ // WEST
- if(board[i][j] == board[x][y]){
- if(board[x][y] == 'X')
- eastwest += 3;
- else if(board[x][y] == 'O')
- eastwest -= 3;
- }else if(board[i][j] == ' '){
- if(board[x][y] == 'X')
- eastwest += 1;
- else if(board[x][y] == 'O')
- eastwest -= 1;
- }else{
- eastwest = 0;
- }
- }
- for(i = x - 1, j = y - 1; i >= 0 && j >= 0; i--, j--){ // NORTHWEST
- if(board[i][j] == board[x][y]){
- if(board[x][y] == 'X')
- northwestsoutheast += 4;
- else if(board[x][y] == 'O')
- northwestsoutheast -= 4;
- }else if(board[i][j] == ' '){
- if(board[x][y] == 'X')
- northwestsoutheast += 2;
- else if(board[x][y] == 'O')
- northwestsoutheast -= 2;
- }else{
- northwestsoutheast = 0;
- }
- }
- for(i = x + 1, j = y + 1; i < size && j < size; i++, j++){ // SOUTHEAST
- if(board[i][j] == board[x][y]){
- if(board[x][y] == 'X')
- northwestsoutheast += 4;
- else if(board[x][y] == 'O')
- northwestsoutheast -= 4;
- }else if(board[i][j] == ' '){
- if(board[x][y] == 'X')
- northwestsoutheast += 2;
- else if(board[x][y] == 'O')
- northwestsoutheast -= 2;
- }else{
- northwestsoutheast = 0;
- }
- }
- for(i = x - 1, j = y + 1; i >= 0 && j < size; i--, j++){ // NORTHEAST
- if(board[i][j] == board[x][y]){
- if(board[x][y] == 'X')
- northeastsouthwest += 4;
- else if(board[x][y] == 'O')
- northeastsouthwest -= 4;
- }else if(board[i][j] == ' '){
- if(board[x][y] == 'X')
- northeastsouthwest += 2;
- else if(board[x][y] == 'O')
- northeastsouthwest -= 2;
- }else{
- northeastsouthwest = 0;
- }
- }
- for(i = x + 1, j = y - 1; i < size && j >= 0; i++, j--){ // SOUTHWEST
- if(board[i][j] == board[x][y]){
- if(board[x][y] == 'X')
- northeastsouthwest += 4;
- else if(board[x][y] == 'O')
- northeastsouthwest -= 4;
- }else if(board[i][j] == ' '){
- if(board[x][y] == 'X')
- northeastsouthwest += 2;
- else if(board[x][y] == 'O')
- northeastsouthwest -= 2;
- }else{
- northeastsouthwest = 0;
- }
- }
- if(northsouth == 1 || northsouth == -1 || northsouth == 3 || northsouth == -3){
- northsouth = 0;
- }
- if(eastwest == 1 || eastwest == -1 || northsouth == 3 || northsouth == -3){
- eastwest = 0;
- }
- if(northwestsoutheast == 2 || northwestsoutheast == -2 || northwestsoutheast == 4 || northwestsoutheast == -4){
- northwestsoutheast = 0;
- }
- if(northeastsouthwest == 2 || northeastsouthwest == -2 || northeastsouthwest == 4 || northeastsouthwest == -4){
- northeastsouthwest = 0;
- }
- //printf("%c | NS: %d EW: %d NWSE: %d NESW: %d\n",board[x][y],northsouth,eastwest,northwestsoutheast,northeastsouthwest);
- return northsouth+eastwest+northwestsoutheast+northeastsouthwest;
- }
- moveEval evaluateWhiteMove(char board[size][size], int x, int y){
- moveEval move;
- int i,j;
- move.score = 0;
- move.x = x;
- move.y = y;
- board[move.x][move.y] = 'X';
- if(checkWin(board) == 'X')
- move.score += 2;
- else if(checkWin(board) == 'O')
- move.score -= 2;
- for(i=0;i<size;i++)
- for(j=0;j<size;j++)
- if(board[i][j] != ' ')
- move.score += checkScore(board,i,j);
- printf("\n(%d , %d) = %d\n",move.x,move.y,move.score);
- board[move.x][move.y] = ' ';
- return move;
- }
- void whiteMove(char board[size][size]){
- int i,j;
- moveEval move;
- moveEval checkmove;
- int scorecheck;
- int first = 1;
- for(i=0;i<size;i++){
- for(j=0;j<size;j++){
- if(board[i][j] == ' '){
- checkmove = evaluateWhiteMove(board,i,j);
- scorecheck = checkmove.score;
- if(first){
- move = checkmove;
- first--;
- }
- if(move.score <= scorecheck){
- move = checkmove;
- }
- }
- }
- }
- board[move.x][move.y] = 'X';
- }
- moveEval evaluateBlackMove(char board[size][size], int x, int y){
- moveEval move;
- int i,j;
- move.score = 0;
- move.x = x;
- move.y = y;
- board[move.x][move.y] = 'O';
- if(checkWin(board) == 'X')
- move.score += 2;
- else if(checkWin(board) == 'O')
- move.score -= 2;
- for(i=0;i<size;i++)
- for(j=0;j<size;j++)
- if(board[i][j] != ' ')
- move.score += checkScore(board,i,j);
- printf("\n(%d , %d) = %d\n",move.x,move.y,move.score);
- board[move.x][move.y] = ' ';
- return move;
- }
- void blackMove(char board[size][size]){
- int i,j;
- moveEval move;
- moveEval checkmove;
- int scorecheck;
- int first = 1;
- for(i=0;i<size;i++){
- for(j=0;j<size;j++){
- if(board[i][j] == ' '){
- checkmove = evaluateBlackMove(board,i,j);
- scorecheck = checkmove.score;
- if(first){
- move = checkmove;
- first--;
- }
- if(move.score >= scorecheck){
- move = checkmove;
- }
- }
- }
- }
- printf("Before %d %d \n",move.x,move.y);
- board[move.x][move.y] = 'O';
- printf("After\n");
- }
- int main(){
- char board[size][size];
- int i,j;
- for(i = 0 ; i < size ; i++)
- for(j = 0 ; j < size ; j++)
- board[i][j] = ' ';
- printf("## XO ##\n");
- board[1][1] = 'X';
- board[2][0] = 'O';
- while(checkBoard(board) && checkWin(board) == ' '){
- printBoard(board);
- whiteMove(board);
- printBoard(board);
- blackMove(board);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement