900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > c语言大作业井字棋程序 C语言实现的井字棋

c语言大作业井字棋程序 C语言实现的井字棋

时间:2020-11-14 22:16:47

相关推荐

c语言大作业井字棋程序 C语言实现的井字棋

/*

*文件名:#.c

*用途:井字棋程序

*编程环境:WinXPSP2+CL8.0

*完成日期:.8Ver0.01

*作者:88250

*联系方式:E-mail:DL88250@QQ:845765

*/

#include

<

stdio.h

>

#define

P11

#define

P2-1

#define

SIZE3

#define

WIN-1

#define

UNWIN0

#define

PEACE1

#define

chkAndPutDwnRow(row,col){

for

(col

=

0

;col

<

SIZE;col

++

){

if

(chsman[row][col]

==

0

){

chsman[row][col]

=

P2;

dsply();

return

;

}

}

}

#define

chkAndPutDwnCol(row,col){

for

(col

=

0

;col

<

SIZE;col

++

){

if

(chsman[col][row]

==

0

){

chsman[col][row]

=

P2;

dsply();

return

;

}

}

}

#define

chkAndPutDwn_Slsh(row,col){

if

(chsman[row][col]

==

0

){

chsman[row][col]

=

P2;

dsply();

return

;

}

}

int

chsman[SIZE][SIZE]

=

{

0

};

/*

functionprototype(declaration).

*/

int

enterChsman(

int

,

int

);

void

dsply(

void

);

void

input(

void

);

void

judge(

void

);

int

chkWin(

void

);

int

chkPeace(

void

);

/*

createaglobalvariablenamedstepFlgusetonotethestep.

*/

int

stepFlg

=

0

;

int

enterChsman(

int

row,

int

col)

{

/*

outofsize.

*/

if

(row

>=

SIZE

||

col

>=

SIZE)

return

0

;

/*

thepiontedlocationisnotempty.

*/

if

(chsman[row][col]

!=

0

)

return

0

;

/*

okay,putdownthechessman.

*/

chsman[row][col]

=

P1;

return

1

;

}

/*

userinput.

*/

void

input(

void

)

{

int

row,col;

do

{

printf(

"

Pleaselocateyourchessman:

"

);

printf(

"

Locationrow:

"

);

scanf(

"

%d

"

,

&

row);

printf(

"

Locationcolumn:

"

);

scanf(

"

%d

"

,

&

col);

if

(enterChsman(row

-

1

,col

-

1

)

==

1

){

printf(

"

Youlocatedchessmanat[%d][%d].

"

,row,col);

dsply();

break

;

}

else

printf(

"

Error:Youputthechesstoawronglocation

"

);

}

while

(

1

);

return

;

}

/*

computerjudgeandinput.

*/

void

judge(

void

)

{

int

row,col;

int

i;

/*

therisklevelstatusofthechessboard.

*/

/*

theattacklevelstatusofthechessboard.

*/

int

rskAndAtkLevlRow[SIZE]

=

{

0

},rskAndAtkLevlCol[SIZE]

=

{

0

},rskAndAtkLevlSlsh[

2

]

=

{

0

};

/*

obviatethespecialsatusofthefirstchessman

*/

if

(stepFlg

==

0

){

/*

now,flagthefirstchessmanhaddown.

*/

stepFlg

=

1

;

if

(chsman[

1

][

1

]

==

P1){

chsman[

0

][

0

]

=

P2;

printf(

"

Thecomputerlocatedchessmanat[1][1].

"

);

dsply();

return

;

}

else

{

chsman[

1

][

1

]

=

P2;

printf(

"

Thecomputerlocatedchessmanat[2][2].

"

);

dsply();

return

;

}

}

/*

notethenumberofthestep.

*/

stepFlg

++

;

if

(stepFlg

==

2

){

if

((chsman[

0

][

0

]

==

P1

&&

chsman[

2

][

2

]

==

P1)

||

(chsman[

0

][

2

]

==

P1

&&

chsman[

2

][

0

]

==

P1)){

chsman[

0

][

1

]

=

P2;

printf(

"

Thecomputerlocatedchessmanat[1][2].

"

);

dsply();

return

;

}

}

/*

evaluatetherisklevelandattacklevelofeveryrow,columnandslash.

*/

for

(row

=

0

;row

<

SIZE;row

++

){

for

(col

=

0

;col

<

SIZE;col

++

){

rskAndAtkLevlRow[row]

+=

chsman[row][col];

}

}

for

(col

=

0

;col

<

SIZE;col

++

){

for

(row

=

0

;row

<

SIZE;row

++

){

rskAndAtkLevlCol[col]

+=

chsman[row][col];

}

}

rskAndAtkLevlSlsh[

0

]

=

chsman[

0

][

0

]

+

chsman[

1

][

1

]

+

chsman[

2

][

2

];

rskAndAtkLevlSlsh[

1

]

=

chsman[

0

][

2

]

+

chsman[

1

][

1

]

+

chsman[

2

][

0

];

/*

attck!

*/

/*

attckarow.

*/

for

(i

=

0

;i

<

SIZE;i

++

){

if

(rskAndAtkLevlRow[i]

==

-

2

){

chkAndPutDwnRow(i,col)

}

}

/*

attckacolumn.

*/

for

(i

=

0

;i

<

SIZE;i

++

){

if

(rskAndAtkLevlCol[i]

==

-

2

){

chkAndPutDwnCol(i,col)

}

}

/*

attackslash().

*/

if

(rskAndAtkLevlSlsh[

0

]

==

-

2

){

for

(row

=

0

,col

=

0

;row

<

SIZE;row

++

,col

++

){

chkAndPutDwn_Slsh(row,col)

}

}

/*

attackslash(/).

*/

if

(rskAndAtkLevlSlsh[

1

]

==

-

2

){

for

(row

=

0

,col

=

2

;row

<

SIZE;row

++

,col

--

){

chkAndPutDwn_Slsh(row,col)

}

}

/*

locatetheriskgridandputdownonechessmantoresolveit.

*/

/*

resolvetheriskofaRow.

*/

for

(i

=

0

;i

<

SIZE;i

++

){

if

(rskAndAtkLevlRow[i]

==

2

){

chkAndPutDwnRow(i,col)

}

}

/*

resolvetheriskofacolumn.

*/

for

(i

=

0

;i

<

SIZE;i

++

){

if

(rskAndAtkLevlCol[i]

==

2

){

chkAndPutDwnCol(i,col)

}

}

/*

resolvetheriskofaslash().

*/

if

(rskAndAtkLevlSlsh[

0

]

==

2

){

for

(row

=

0

,col

=

0

;row

<

SIZE;row

++

,col

++

){

chkAndPutDwn_Slsh(row,col)

}

}

/*

resolvetheriskofaslash(/).

*/

if

(rskAndAtkLevlSlsh[

1

]

==

2

){

for

(row

=

0

,col

=

2

;row

<

SIZE;row

++

,col

--

){

chkAndPutDwn_Slsh(row,col)

}

}

/*

ifthereisnoriskexist,putdownthechessmaninablank(isnotthebestblank,maybe).

*/

for

(row

=

0

;row

<

SIZE;row

++

){

for

(col

=

0

;col

<

SIZE;col

++

){

if

(chsman[row][col]

==

0

&&

((row

==

0

&&

col

==

0

)

||

(row

==

0

&&

col

==

2

)

||

(row

==

2

&&

col

==

0

)

||

(row

==

2

&&

col

==

2

))){

chsman[row][col]

=

P2;

dsply();

return

;

}

}

}

}

/*

displaythecurrentchessmanboard.

*/

void

dsply(

void

)

{

int

row,col,i;

/*

printthehead.

*/

for

(i

=

0

;i

<

SIZE

*

4

+

1

;i

++

)

printf(

"

-

"

);

printf(

"

"

);

/*

printthecontect.

*/

for

(row

=

0

;row

<

SIZE;row

++

){

printf(

"

|

"

);

for

(col

=

0

;col

<

SIZE;col

++

){

if

(chsman[row][col]

==

P1)printf(

"

o|

"

);

else

if

(chsman[row][col]

==

P2)printf(

"

x|

"

);

else

printf(

"

|

"

);

}

printf(

"

"

);

/*

printthefloor.

*/

for

(i

=

0

;i

<

SIZE

*

4

+

1

;i

++

)

printf(

"

-

"

);

printf(

"

"

);

}

return

;

}

/*

checkwhetherwinthisgame.

*/

int

chkWin(

void

)

{

int

i;

for

(i

=

0

;i

<

SIZE;i

++

){

if

(chsman[i][

0

]

+

chsman[i][

1

]

+

chsman[i][

2

]

==

-

3

||

chsman[

0

][i]

+

chsman[

1

][i]

+

chsman[

2

][i]

==

-

3

||

chsman[

0

][

0

]

+

chsman[

1

][

1

]

+

chsman[

2

][

2

]

==

-

3

||

chsman[

0

][

2

]

+

chsman[

1

][

1

]

+

chsman[

2

][

0

]

==

-

3

){

return

WIN;

}

}

return

UNWIN;

}

/*

checkwhetherpeacewithuser.

*/

int

chkPeace(

void

)

{

int

row,col;

int

sum

=

0

;

for

(row

=

0

;row

<

SIZE;row

++

){

for

(col

=

0

;col

<

SIZE;col

++

){

if

(sum

+=

chsman[row][col]

==

PEACE){

return

PEACE;

}

}

}

return

0

;

}

int

main(

char

*

args[])

{

/*

displaythechessboard.

*/

dsply();

do

{

/*

user'sturnofinput.

*/

input();

/*

computersays:itismyturnofinput.

*/

judge();

if

(chkWin()

==

WIN)

break

;

if

(stepFlg

==

5

&&

chkPeace()

==

PEACE){

printf(

"

Peace!

"

);

return

0

;

}

}

while

(

1

);

printf(

"

Hehe....Iwinthisgame~:-p

"

);

return

0

;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。