Assignment 1 1
Assignment 1 1
Roll No: 26
Prn No:12010017
Division: TY_C
TicTactToe:
1. Non-Ai Implementation:
Code:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <ctime>
int main(){
int board[9] = {0,0,0,0,0,0,0,0,0};
int possibleWinner;
int move;
bool isInvalid;
string moveString;
srand((int) time(0));
int corners[4] = {0,2,6,8};
int sides[4] = {1,3,5,7};
while (true) {
do {
cout << "X: ";
getline(cin, moveString);
move = moveString[0] - '1';
if (move > 8 || move < 0 || board[move] != 0) {
cout << "Invalid input" << endl;
isInvalid = true;
} else {
board[move] = 1;
isInvalid = false;
cout << endl;
}
} while (isInvalid);
if (Over(board) > 0) {
outputBoard(board);
break;
}
bool good = false;
for (int x = 2; x > 0; x--){
possibleWinner = willWin(board, x);
if (possibleWinner != -1) {
board[possibleWinner] = 2;
good = true;
break;
}
}
if (good);
else if (board[4] == 0) board[4] = 2;
else if (exceptionalCase(board) > -1) board[exceptionalCase(board)] =
2;
else if (getSpace(board, corners) != -1) board[getSpace(board,
corners)] = 2;
else board[getSpace(board, sides)] = 2; //Sides
outputBoard(board);
if(Over(board)) break;
}
return 0;
}
Output:
2. Ai implementation:
Code:
#include<iostream>
#include<iomanip>
#include<conio.h>
using namespace std;
#define COMPUTER 1
#define HUMAN 2
#define SIDE 3
#define COMPUTERMOVE 'O'
#define HUMANMOVE 'X'
void Instructions()
{
printf("\nChoose a cell numbered from 1 to 9 as below and play\n\n");
printf("\t\t\t 1 | 2 | 3 \n");
printf("\t\t\t-----------\n");
printf("\t\t\t 4 | 5 | 6 \n");
printf("\t\t\t-----------\n");
printf("\t\t\t 7 | 8 | 9 \n\n");
}
void Winner(int Turn)
{
if (Turn == COMPUTER)
printf("COMPUTER has won\n");
else
printf("HUMAN has won\n");
}
return(false);
}
initialise(board);
Instructions();
while (gameOver(board) == false && moveIndex != SIDE*SIDE)
{
int v;
if (whoseTurn == COMPUTER)
{
v =Movecalc(board, moveIndex);
x = v / SIDE;
y = v % SIDE;
board[x][y] = COMPUTERMOVE;
printf("COMPUTER has put a %c in cell %d\n\n", COMPUTERMOVE, v+1);
Board(board);
moveIndex ++;
whoseTurn = HUMAN;
}
else if (whoseTurn == HUMAN)
{
printf("You can insert in the following positions : ");
for(int i=0; i<SIDE; i++)
for (int j = 0; j < SIDE; j++)
if (board[i][j] == ' ')
printf("%d ", (i * 3 + j) + 1);
printf("\n\nEnter the position = ");
scanf("%d",&v);
v--;
x = v / SIDE;
y = v % SIDE;
if(board[x][y] == ' ' && v<9 && v>=0)
{
board[x][y] = HUMANMOVE;
printf ("\nHUMAN has put a %c in cell %d\n\n", HUMANMOVE,
v+1);
Board(board);
moveIndex ++;
whoseTurn = COMPUTER;
}
else if(board[x][y] != ' ' && v<9 && v>=0)
{
printf("\nPosition is occupied, select any one place from the
available places\n\n");
}
else if(v<0 || v>8)
{
printf("Invalid position\n");
}
}
}
if (gameOver(board) == false && moveIndex == SIDE * SIDE)
printf("It's a draw\n");
else
{
if (whoseTurn == COMPUTER)
whoseTurn = HUMAN;
else if (whoseTurn == HUMAN)
whoseTurn = COMPUTER;
Winner(whoseTurn);
}
}
int main()
{
printf("\
n-------------------------------------------------------------------\n\n");
printf("\t\t\t Tic-Tac-Toe\n");
printf("\
n-------------------------------------------------------------------\n\n");
char cont='y';
do {
char choice;
printf("Do you want to start first?(y/n) : ");
scanf(" %c", &choice);
if(choice=='n')
playTicTacToe(COMPUTER);
else if(choice=='y')
playTicTacToe(HUMAN);
else
printf("Invalid choice\n");
printf("\nDo you want to quit(y/n) : ");
scanf(" %c", &cont);
} while(cont=='n');
return (0);
}
Output: