Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import std.stdio; //For console I/O
- import std.random; //We need this for the randomness of the CPU move
- import std.process; //For clearing the console screen - system("cls")
- char[3][3] board = [['1','2','3'],['4','5','6'],['7','8','9']]; //The X/O board
- struct Player //The player structure. Contains player's mark and win data.
- {
- char[1] mark;
- bool winner;
- int inp;
- }
- void printBoard() //Function to print the board
- {
- writeln(" ",board[0][0]," | ",board[0][1]," | ",board[0][2]);
- writeln("---","|","---","|","---");
- writeln(" ",board[1][0]," | ",board[1][1]," | ",board[1][2]);
- writeln("---","|","---","|","---");
- writeln(" ",board[2][0]," | ",board[2][1]," | ",board[2][2]);
- }
- bool checkWin(Player who) //Function to check the winner
- {
- if(board[0][0] == who.mark[0] && board[1][1] == who.mark[0] && board[2][2] == who.mark[0])
- {
- return true;
- }
- else if(board[0][2] == who.mark[0] && board[1][1] == who.mark[0] && board[2][0] == who.mark[0])
- {
- return true;
- }
- else if(board[0][0] == who.mark[0] && board[0][1] == who.mark[0] && board[0][2] == who.mark[0])
- {
- return true;
- }
- else if(board[1][0] == who.mark[0] && board[1][1] == who.mark[0] && board[1][2] == who.mark[0])
- {
- return true;
- }
- else if(board[2][0] == who.mark[0] && board[2][1] == who.mark[0] && board[2][2] == who.mark[0])
- {
- return true;
- }
- else if(board[0][0] == who.mark[0] && board[1][0] == who.mark[0] && board[2][0] == who.mark[0])
- {
- return true;
- }
- else if(board[0][1] == who.mark[0] && board[1][1] == who.mark[0] && board[2][1] == who.mark[0])
- {
- return true;
- }
- else if(board[0][2] == who.mark[0] && board[1][2] == who.mark[0] && board[2][2] == who.mark[0])
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- int setMark(Player who) //Function to set the mark on the board
- {
- int input = who.inp-1;
- switch(input)
- {
- case 0:
- if(board[0][0] == 'X' || board[0][0] == 'O')
- {
- return 1;
- }
- board[0][0] = who.mark[0];
- break;
- case 1:
- if(board[0][1] == 'X' || board[0][1] == 'O')
- {
- return 1;
- }
- board[0][1] = who.mark[0];
- break;
- case 2:
- if(board[0][2] == 'X' || board[0][2] == 'O')
- {
- return 1;
- }
- board[0][2] = who.mark[0];
- break;
- case 3:
- if(board[1][0] == 'X' || board[1][0] == 'O')
- {
- return 1;
- }
- board[1][0] = who.mark[0];
- break;
- case 4:
- if(board[1][1] == 'X' || board[1][1] == 'O')
- {
- return 1;
- }
- board[1][1] = who.mark[0];
- break;
- case 5:
- if(board[1][2] == 'X' || board[1][2] == 'O')
- {
- return 1;
- }
- board[1][2] = who.mark[0];
- break;
- case 6:
- if(board[2][0] == 'X' || board[2][0] == 'O')
- {
- return 1;
- }
- board[2][0] = who.mark[0];
- break;
- case 7:
- if(board[2][1] == 'X' || board[2][1] == 'O')
- {
- return 1;
- }
- board[2][1] = who.mark[0];
- break;
- case 8:
- if(board[2][2] == 'X' || board[2][2] == 'O')
- {
- return 1;
- }
- board[2][2] = who.mark[0];
- break;
- default:
- return 1;
- break;
- }
- return 0;
- }
- void declareWinner(Player p1, Player p2) //Function to declare winner
- {
- if(p1.winner)
- {
- writeln("\n***CONGRATS TO YOU FOR WINNING!***\n");
- }
- else if(p2.winner)
- {
- writeln("\n***CONGRATS TO CPU/P2 FOR WINNING!***\n");
- }
- }
- int main() //The main function
- {
- Player player1; //Set up player 1 variables
- player1.mark[0] = 'X';
- player1.winner = false;
- player1.inp = 12;
- Player cpu; //Set up player 2 (cpu) variables
- cpu.mark[0] = 'O';
- cpu.winner = false;
- while(true) //Main loop
- {
- system("cls");
- writeln("Copyright(c) 2014 - Ahnaf Tahmid\n");
- writefln("Player 1 (You) = %s, CPU = %s\n",player1.mark[0],cpu.mark[0]);
- printBoard();
- while(setMark(player1) == 1)
- {
- write("\nEnter a position: ");
- readf(" %s", &player1.inp);
- if(setMark(player1) == 1)
- {
- writeln("\nInvalid position!");
- }
- else
- {
- break;
- }
- }
- player1.winner = checkWin(player1);
- if(player1.winner)
- {
- system("cls");
- writeln("Copyright(c) 2014 - Ahnaf Tahmid\n");
- writeln("Player 1 (You) = X, CPU = O\n");
- printBoard();
- declareWinner(player1, cpu);
- break;
- }
- cpu.inp = uniform(0,9)+1;
- int errors = 0; //Draw indicator
- while(setMark(cpu) == 1 && errors < 20)
- {
- cpu.inp = uniform(0,9)+1;
- errors++;
- }
- cpu.winner = checkWin(cpu);
- if(cpu.winner)
- {
- system("cls");
- writeln("Copyright(c) 2014 - Ahnaf Tahmid\n");
- writeln("Player 1 (You) = X, CPU = O\n");
- printBoard();
- declareWinner(player1, cpu);
- break;
- }
- if(errors == 20) //This is actually the real draw handling (not the best one around)
- {
- writeln("\n***ITS A DRAW!***\n");
- break;
- }
- }
- writeln("Enter any number to exit.");
- int dummy_var;
- readf(" %s", &dummy_var);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement