Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#includ? #includ? using nam?spac? std; c?nst int SIZ?=3; // d?clar? all th? functi?ns h?r? int main() { char turn; int x, y; char gam?B?ard[SIZ?][SIZ?]; //

#includ?

#includ?

using nam?spac? std;

c?nst int SIZ?=3;

// d?clar? all th? functi?ns h?r?

int main()

{

char turn;

int x, y;

char gam?B?ard[SIZ?][SIZ?];

// Call th? functi?n Initializ?B?ard t? Initializ? th? gam? b?ard pi?c?s t? blanks

Initializ?B?ard(gam?B?ard);

// Rand?mly d?t?rmin? wh? is t? start th? gam?

int t=rand()%2;

if (t == 1)

turn='X';

?ls?

turn='?';

c?ut << "Gam? Starts!" << ?ndl;

// Display th? initial gam? b?ard ?n scr??n

Display(gam?B?ard);

// Th? gam? is play?d b?tw??n tw? play?rs until ?n? ?f tw? cas?s ?ccurs:

// (1) th? b?ard is c?mpl?t?ly fill?d with pi?c?s fr?m th? play?r, and n? winn?r is d?t?rmin?d.

// (2) ?n? play?r wins th? gam?

whil? (!B?ardIsFull(gam?B?ard)&& (Ch?ckW?n(gam?B?ard)=='n'))

{

// Pr?mpt th? play?r t? ?nt?r th? p?siti?n t? play th? n?xt m?v?

// Th? m?v? is validat?d in th? inn?r l??p.

// ?nly if a m?v? is valid, it will b? assign?d t? th? gam?b?ard

d?

{

d? {

c?ut << "Play?r " << turn << " mak?s th? n?xt m?v?."<

c?ut << "?nt?r th? x, y l?cati?n, 0<=x<3, 0<=y<3:";

cin >> x >> y;

} whil? (x>2 || x<0 || y>2 || y<0);

}

whil? (!Assign(gam?B?ard, x, y, turn));

// Th? Assign functi?n will att?mpt t? assign a m?v? fr?m a play?r t? th? gam?b?ard. IF th?r? is alr?ady a pi?c? at that l?cati?n, Assign functi?n will n?t put th? pi?c? at that l?cati?n and it r?turns fals?. ?th?rwis?, it will put th? pi?c? at that l?cati?n and r?turn tru?.

// Sh?w th? updat?d gam? b?ard

Display(gam?B?ard);

// d?t?rmin?s th? play?r f?r th? n?xt m?v?

if (turn == 'X')

turn = '?';

?ls?

turn = 'X';

}

// Display th? r?sult ?f th? gam?

if (Ch?ckW?n(gam?B?ard) == 'X')

c?ut << "Play?r X wins!" << ?ndl;

?ls? if (Ch?ckW?n(gam?B?ard) == '?')

c?ut << "Play?r ? wins!" << ?ndl;

?ls?

c?ut << "This is a Draw gam?!" << ?ndl;

r?turn 0;

}

// This functi?n initializ?s all th? ?l?m?nts ?f th? gam? b?ard t? blank charact?rs ' '

v?id Initializ?B?ard(char gam?B?ard [][SIZ?])

{

}

// This functi?n displays th? gam? b?ard as a 3 by 3 b?ard

v?id Display(char gam?B?ard[][SIZ?])

{

}

// This functi?n ch?cks t? s?? if all th? ?l?m?nts ?f th? gam? b?ard hav? b??n fill?d

b??l B?ardIsFull(char gam?B?ard[][SIZ?])

{

}

// This functi?n assigns charact?r p t? l?cati?n (x, y) ?n th? gam? b?ard

// p is ?ith?r 'X' ?r '?'

// If th? l?cati?n (x, y) alr?ady has a pi?c?/charact?r th?r?, d? n?t assign p t? that l?cati?n, r?turns th? b??l?an valu? fals?

// If th? l?cati?n (x, y) d??s n?t hav? any pi?c?/charact?r, assign p t? that l?cati?n and r?turn tru?

b??l Assign(char gam?B?ard[][SIZ?], int x, int y, char p)

{

}

// ch?ck t? s?? if th? gam? is ?v?r by ch?cking

// if any r?w, c?lumn ?r diag?nal has th? sam? charact?r

// if 'X' is ?n all ?l?m?nts ?f a r?w, c?lumn, ?r diag?nal, 'X' is r?turn?d

// if '?' is ............................................., '?' is r?turn?d

// ?th?rwis?, 'n' is r?turn?d.

char Ch?ckW?n(char gam?B?ard[][SIZ?])

{

char w?n = 'n';

// writ? multiway if stat?m?nt t? ch?ck wh?th?r a win c?nditi?n is m?t

// Cas? 1: th? 3 ?l?m?nts ?n th? i-th r?w ?f th? array ar? th? sam?, assign th? array ?l?m?nt valu? t? variabl? "w?n"

// Cas? 2: th? 3 ?l?m?nts ?n th? j-th c?lumn ?f th? array ar? th? sam?, assign valu? t? variabl? "w?n"

// Cas? 3: th? 3 ?l?m?nts ?n th? diag?nal ?f th? array ar? th? sam?, assign valu? t? variabl? "w?n"

// Cas? 4: th? 3 ?l?m?nts ?f th? s?c?nd diag?nal ?f th? array ar? th? sam?, assign valu? t? variabl? "w?n"

r?turn w?n;

}

Sp?cial R?quir?m?nts Y?u ar? r?quir?d t? c?mpl?t? th? f?ll?wing f?ur functi?ns:

Initializ?B?ard: This functi?n initializ? all th? ?l?m?nts ?f th? gam? b?ard t? blanks ' '

Display: This functi?n displays th? gam? b?ard as a 3 by 3 b?ard

B?ardIsFull: This functi?n ch?cks t? s?? if all th? ?l?m?nts ?f th? gam? b?ard hav? b??n fill?d

Assign: This functi?n assigns charact?r p t? l?cati?n (x, y) ?n th? gam? b?ard p is ?ith?r 'X' ?r '?'

Ch?ckW?n: This functi?n ch?cks t? s?? if th? gam? is ?v?r by ch?cking if any r?w, c?lumn ?r diag?nal has th? sam? charact?r. If 'X' is ?n all ?l?m?nts ?f a r?w, c?lumn, ?r diag?nal, 'X' is r?turn?d. If '?' is ............................................., '?' is r?turn?d. ?th?rwis?, 'n' is r?turn?d.

H?r? is an ?xampl? ?utput ?f th? pr?gram:

Gam? Starts!

Curr?nt gam? b?ard:

| |

-----------

| |

-----------

| |

Play?r X mak?s th? n?xt m?v?.

?nt?r th? x, y l?cati?n, 0<=x<3, 0<=y<3: 1 1

Curr?nt gam? b?ard:

| |

-----------

| X |

-----------

| |

Play?r ? mak?s th? n?xt m?v?.

?nt?r th? x, y l?cati?n, 0<=x<3, 0<=y<3: 0 2

Curr?nt gam? b?ard:

| | ?

-----------

| X |

-----------

| |

Play?r X mak?s th? n?xt m?v?.

?nt?r th? x, y l?cati?n, 0<=x<3, 0<=y<3: 0 0

Curr?nt gam? b?ard:

X | | ?

-----------

| X |

-----------

| |

Play?r ? mak?s th? n?xt m?v?.

?nt?r th? x, y l?cati?n, 0<=x<3, 0<=y<3: 1 2

Curr?nt gam? b?ard:

X | | ?

-----------

| X | ?

-----------

| |

Play?r X mak?s th? n?xt m?v?.

?nt?r th? x, y l?cati?n, 0<=x<3, 0<=y<3: 2 0

Curr?nt gam? b?ard:

X | | ?

-----------

| X | ?

-----------

X | |

Play?r ? mak?s th? n?xt m?v?.

?nt?r th? x, y l?cati?n, 0<=x<3, 0<=y<3: 2 2

Curr?nt gam? b?ard:

X | | ?

-----------

| X | ?

-----------

X | | ?

Play?r ? wins!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David Kroenke, David Auer, Scott Vandenberg, Robert Yoder

9th Edition

0135188148, 978-0135188149, 9781642087611

More Books

Students also viewed these Databases questions

Question

What is your role within these groups?

Answered: 1 week ago