Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- char grid[3][3];
- void showGrid();
- void clearGrid();
- char checkWinner();
- int getOWin();
- int preventXWin();
- char currentWinner;
- char currentPlayer;
- int main(){
- int location = 0;
- srand(time(0));
- bool legalMove;
- int choice;
- currentPlayer = 'X';
- int tie=0, xWin=0, oWin=0;
- int mode;
- do{
- printf("Enter '1' for single-player mode, or '2' for two-player mode: ");
- scanf("%i",&mode);
- }while((mode != 1)&&(mode != 2));
- do{
- currentWinner = '.';
- clearGrid();
- showGrid();
- do{
- if((mode == 1)&&(currentPlayer=='O')){
- printf("Computer plays.\n");
- location = getOWin();
- if(location == -1){
- location = preventXWin();
- if(location == -1){
- location = rand()%9;
- }
- }
- do{
- if((grid[location/3][location%3]!='X')&&(grid[location/3][location%3]!='O')&&(location>=0)&&(location<=8)){
- legalMove = true;
- }
- else{
- legalMove = false;
- location = rand()%9;
- }
- }while(!legalMove);
- }
- else{
- printf("Player %c, please choose a valid location: ",currentPlayer);
- scanf("%i",&location);
- location--;
- }
- if((grid[location/3][location%3]!='X')&&(grid[location/3][location%3]!='O')&&(location>=0)&&(location<=8)){
- grid[location/3][location%3] = currentPlayer;
- legalMove = true;
- }
- else{
- legalMove = false;
- }
- showGrid();
- if(legalMove){
- currentWinner = checkWinner();
- if(currentPlayer == 'X'){
- currentPlayer = 'O';
- }
- else{
- currentPlayer = 'X';
- }
- }
- else{
- printf("Illegal move.\n");
- }
- }while(currentWinner=='.');
- if(currentWinner=='Z'){
- printf("Game is a tie.\n");
- tie++;
- }
- else{
- printf("Winner of this round is player: %c\n",currentWinner);
- if(currentWinner == 'X'){
- xWin++;
- }
- else{
- oWin++;
- }
- }
- printf("Press '1' to continue, or any other number to quit: ");
- scanf("%i",&choice);
- }while(choice==1);
- printf("X wins: %i, O wins: %i, tie game: %i\n\n", xWin, oWin, tie);
- printf("Press any key followed by enter to exit: ");
- scanf("%c",¤tWinner);
- return 0;
- }
- int getOWin(){
- int i;
- int loc = -1;
- char win;
- char prev;
- for(i = 0; i<9; i++){
- prev = grid[i/3][i%3];
- if((grid[i/3][i%3]!='X')&&(grid[i/3][i%3]!='O')){
- grid[i/3][i%3] = 'O';
- win = checkWinner();
- if(win == 'O'){
- loc = i;
- }
- }
- grid[i/3][i%3] = prev;
- }
- return loc;
- }
- int preventXWin(){
- int i;
- int loc = -1;
- char win;
- char prev;
- for(i = 0; i<9; i++){
- prev = grid[i/3][i%3];
- if((grid[i/3][i%3]!='X')&&(grid[i/3][i%3]!='O')){
- grid[i/3][i%3] = 'X';
- win = checkWinner();
- if(win == 'X'){
- loc = i;
- }
- }
- grid[i/3][i%3] = prev;
- }
- return loc;
- }
- char checkWinner(){
- int i, j;
- char cW = '.';
- for(i=0;i<3;i++){
- if((grid[i][0]==grid[i][1])&&(grid[i][0]==grid[i][2])){
- cW = grid[i][0];
- }
- if((grid[0][i]==grid[1][i])&&(grid[0][i]==grid[2][i])){
- cW = grid[0][i];
- }
- }
- if((grid[0][0]==grid[1][1])&&(grid[0][0]==grid[2][2])){
- cW = grid[0][0];
- }
- if((grid[0][2]==grid[1][1])&&(grid[0][2]==grid[2][0])){
- cW = grid[0][2];
- }
- bool noEmptySpots = true;
- if(cW == '.'){
- for(i=0;i<3;i++){
- for(j=0;j<3;j++){
- if((grid[i][j]!='X')&&(grid[i][j]!='O')){
- noEmptySpots = false;
- }
- }
- }
- if(noEmptySpots){
- cW = 'Z';
- }
- }
- return cW;
- }
- void clearGrid(){
- int i, j;
- for(i=0;i<3;i++){
- for(j=0;j<3;j++){
- grid[i][j] = (i*3)+j+49;
- }
- }
- }
- void showGrid(){
- printf("+---+---+---+\n");
- printf("| %c | %c | %c |\n",grid[0][0],grid[0][1],grid[0][2]);
- printf("+---+---+---+\n");
- printf("| %c | %c | %c |\n",grid[1][0],grid[1][1],grid[1][2]);
- printf("+---+---+---+\n");
- printf("| %c | %c | %c |\n",grid[2][0],grid[2][1],grid[2][2]);
- printf("+---+---+---+\n");
- }
Add Comment
Please, Sign In to add comment