online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code    Language
#include <stdio.h> #include <string.h> char board[10] = "---------" ; int bestmoveg = 0; char win_c(char board[10]); void print_board(char board[10]); void player_move(char board[10]); void computer_move(char board[10]); int minimax(char board[10], char player, int depth); char opposition(char player); void main(){ int end = 0; printf("\n\n\n\t\tNoughts and Crosses\n\t¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬\n\n\t\t\t\t\tby James L.\n\n\n"); while (! end){ print_board(board); player_move(board); if (win_c(board)!='0'){ end = 1; }else{ print_board(board); computer_move(board); if (win_c(board)!='0'){ end = 1; } } } char a = win_c(board); if (a=='O'){ print_board(board); printf("You Win!"); } else if(a=='X'){ print_board(board); printf("You Lose!"); }else{ print_board(board); printf("It was a draw!"); } } void print_board(char board[10]){ printf("\n\n"); for (int c = 0; c<3; c++){ printf("%c %c %c \n", board[c*3], board[c*3+1], board[c*3+2]); } } void player_move(char board[10]){ int x,y; printf("\nPlease enter the coordinates of the space you want to go.\n\n\tHorizontal: "); scanf("%d",&x); printf("\n\tVertical: "); scanf("%d",&y); if (((y-1)*3+(x-1)>-1) && ((y-1)*3+(x-1)<9) && (board[(y-1)*3+(x-1)]=='-')){ board[(y-1)*3+(x-1)] = 'O'; } else{ printf("\n\nInvalid move\n\n\n"); player_move(board); } } char win_c(char board[10]){ for(int plus = 0; plus<3; plus++){ if ((board[plus]==board[plus+3]) && (board[plus]==board[plus+6]) && (board[plus]!='-')){ return board[plus]; } if ((board[plus*3]==board[(plus*3)+1]) && (board[plus*3]==board[(plus*3)+2]) && (board[plus*3]!='-')){ return board[plus*3]; } } if ((board[0]==board[4]) && (board[0]==board[8]) && (board[0]!='-')){ return board[0]; } if ((board[2]==board[4]) && (board[2]==board[6]) && (board[2]!='-')){ return board[2]; } for (int i = 0; i<9; i++){ if (board[i] == '-'){ return '0'; } } return 'D'; } void computer_move(char board[10]){ int depth = 0; char player = 'X'; int computer_mover = minimax(board, player, depth); board[bestmoveg] = 'X'; } char opposition(char player){ if (player=='O'){ return 'X'; } return 'O'; } int minimax(char board[10], char player, int depth){ int bestmove, bestscore, move, score; if (win_c(board)=='X'){ return 10-depth; } if (win_c(board)=='O'){ return -10+depth; } if (win_c(board) == 'D'){ return 0; } int moves[9] = {0}; int scores[9] = {0}; int count = 0; for (int a = 0; a<9; a++){ if (board[a] == '-'){ board[a] = player; score = minimax(board, opposition(player), depth+1); moves[count] = a; scores[count] = score; count++; board[a] = '-'; } } if (player == 'X'){ bestscore = -10; bestmove =-1; for (int c = 0; c<count; c++){ if (scores[c]>bestscore){ bestscore = scores[c]; bestmove = moves[c]; } } } else{ bestscore = 10; bestmove =-1; for (int c = 0; c<count; c++){ if (scores[c]<bestscore){ bestscore = scores[c]; bestmove = moves[c]; } } } bestmoveg = bestmove; return bestscore; }
  • {user}

      {timestamp}

    {content}

    #FunctionFile:Line
    VariableValue
    ExpressionValue