1 Ý tưởng cờ Caro 2009-10-27, 16:33
Gà_rán
Thành Viên
Ai có ý kiến gì thì đóng góp vô nha.Thầy bắt làm bài này mệt thiệt đó.
#include
#include
#include
#include
#include "mouse.h"
char box[3][3];
int rand(int num)
{
randomize();
float n = rand();
n /= (RAND_MAX - 1);
n *= num;
return n;
}
char CheckWin()
{
for(int count = 0; count < 3; count++)
if(box[count][0] == box[count][1] && box[count][1] == box[count][2] && box[count][0] != 0)
break;
if(count != 3) return box[count][0];
for(count = 0; count < 3; count++)
if(box[0][count] == box[1][count] && box[1][count] == box[2][count] && box[0][count] != 0)
break;
if(count != 3) return box[0][count];
if(box[1][1] != 0)
{
if(box[0][0] == box[1][1] && box[1][1] == box[2][2]) return box[1][1];
if(box[2][0] == box[1][1] && box[1][1] == box[0][2]) return box[1][1];
}
for(count = 0; count < 9; count++)
if(box[count / 3][count % 3] == 0) break;
if(count == 9) return 3;
return 0;
}
void ComputerPlay(char x, char y)
{
HideMouse();
setcolor(4);
circle(x * 50 + 125, y * 50 + 125, 20);
ShowMouse();
}
char computer()
{
char x, y, count;
char newsols[9] = {0, 0, 0, 0, 0}, pos[9][2] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}};
char next = -1, couldwin, xx = -1;
for(y = 0; y < 3; y++)
{
couldwin = 0;
xx = -1;
for(count = 0; count < 3; count++)
if(box[y][count] == 2) couldwin++;
else if(box[y][count] == 0) xx = count;
if(couldwin == 2 && xx != -1)
{
box[y][xx] = 2;
ComputerPlay(xx, y);
return CheckWin();
}
}
for(y = 0; y < 3; y++)
{
couldwin = 0;
xx = -1;
for(count = 0; count < 3; count++)
if(box[count][y] == 2) couldwin++;
else if(box[count][y] == 0) xx = count;
if(couldwin == 2 && xx != -1)
{
box[xx][y] = 2;
ComputerPlay(y, xx);
return CheckWin();
}
}
couldwin = 0;
xx = -1;
for(x = 0; x < 3; x++)
{
if(box[x][x] == 2) couldwin++;
else if(box[x][x] == 0) xx = x;
}
if(couldwin == 2 && xx != -1)
{
box[xx][xx] = 2;
ComputerPlay(xx, xx);
return CheckWin();
}
couldwin = 0;
xx = -1;
for(x = 0; x < 3; x++)
{
if(box[x][2 - x] == 2) couldwin++;
else if(box[x][2 - x] == 0) xx = x;
}
if(couldwin == 2 && xx != -1)
{
box[xx][2 - xx] = 2;
ComputerPlay(2 - xx, xx);
return CheckWin();
}
for(y = 0; y < 3; y++)
{
couldwin = 0;
xx = -1;
for(count = 0; count < 3; count++)
if(box[y][count] == 1) couldwin++;
else if(box[y][count] == 0) xx = count;
if(couldwin == 2 && xx != -1)
{
box[y][xx] = 2;
ComputerPlay(xx, y);
return CheckWin();
}
}
for(y = 0; y < 3; y++)
{
couldwin = 0;
xx = -1;
for(count = 0; count < 3; count++)
if(box[count][y] == 1) couldwin++;
else if(box[count][y] == 0) xx = count;
if(couldwin == 2 && xx != -1)
{
box[xx][y] = 2;
ComputerPlay(y, xx);
return CheckWin();
}
}
couldwin = 0;
xx = -1;
for(x = 0; x < 3; x++)
{
if(box[x][x] == 1) couldwin++;
else if(box[x][x] == 0) xx = x;
}
if(couldwin == 2 && xx != -1)
{
box[xx][xx] = 2;
ComputerPlay(xx, xx);
return CheckWin();
}
couldwin = 0;
xx = -1;
for(x = 0; x < 3; x++)
{
if(box[x][2 - x] == 1) couldwin++;
else if(box[x][2 - x] == 0) xx = x;
}
if(couldwin == 2 && xx != -1)
{
box[xx][2 - xx] = 2;
ComputerPlay(2 - xx, xx);
return CheckWin();
}
for(x = 0; x < 3; x++)
for(y = 0; y < 3; y++)
{
next++;
if(box[y][x] != 0) continue;
newsols[next]++;
for(count = 0; count < 3; count++)
if(box[y][count] == 1) break;
if(count == 3) newsols[next]++;
for(count = 0; count < 3; count++)
if(box[count][x] == 1) break;
if(count == 3) newsols[next]++;
if(x == y)
{
for(count = 0; count < 3; count++)
if(box[count][count] == 1) break;
if(count == 3) newsols[next]++;
}
if(x == 2 - y)
{
for(count = 0; count < 3; count++)
if(box[2 - count][count] == 1) break;
if(count == 3) newsols[next]++;
}
pos[next][0] = x;
pos[next][1] = y;
}
x = newsols[0];
y = 0;
for(count = 1; count < 9; count++)
if(newsols[count] > x)
{
x = newsols[count];
y = count;
}
char count2 = 0;
for(count = 0; count < 9; count++)
if(newsols[count] == x) count2++;
char play = rand(count2);
count2 = -1;
for(count = 0; count < 9; count++)
{
if(newsols[count] == x) count2++;
if(count2 == play)
{
y = count;
break;
}
}
box[pos[y][1]][pos[y][0]] = 2;
ComputerPlay(pos[y][0], pos[y][1]);
return CheckWin();
}
char player(char pl)
{
int x, y;
while(1)
{
if(kbhit())
{
if(getch() == 27)
{
closegraph();
exit(1);
}
}
if(GetButton() == LEFT_BUTTON)
{
ResetMouse();
x = GetXMouse();
y = GetYMouse();
if(x < 100 || y < 100 || x >= 250 || y >= 250) continue;
x -= 100;
x /= 50;
y -= 100;
y /= 50;
if(box[y][x] == 0) break;
}
}
box[y][x] = pl;
sound(3000);
delay(50);
nosound();
setcolor(2);
HideMouse();
if(pl == 1)
{
line(x * 50 + 105, y * 50 + 105, x * 50 + 145, y * 50 + 145);
line(x * 50 + 105, y * 50 + 145, x * 50 + 145, y * 50 + 105);
}
else ComputerPlay(x, y);
ShowMouse();
return CheckWin();
}
void DrawBox()
{
int x, y;
setfillstyle(1, 7);
HideMouse();
bar(100, 100, 250, 250);
setcolor(1);
setlinestyle(0, 0, 3);
for(x = 100; x <= 250; x += 50)
line(x, 100, x, 250);
for(y = 100; y <= 250; y += 50)
line(100, y, 250, y);
ShowMouse();
}
void WriteScores(int s1, int s2)
{
setfillstyle(1, 7);
bar(0, 400, getmaxx(), getmaxy());
char tmp[100];
settextstyle(1, 0, 1);
setcolor(5);
sprintf(tmp, "Player1: %d", s1);
outtextxy(50, 400, tmp);
sprintf(tmp, "Player2: %d", s2);
outtextxy(50, 430, tmp);
}
char Menu()
{
settextstyle(1, 0, 1);
HideMouse();
outtextxy(50, 10, "1.Play with computer.");
outtextxy(50, 30, "2.Play with human.");
char key = 0;
while(key != 27)
{
key = getch();
if(key == '1' || key == '2') break;
}
setfillstyle(1, 7);
bar(50, 10, 400, 60);
ShowMouse();
if(key == 27)
{
closegraph();
exit(1);
}
return key - 48;
}
void main()
{
char key = 0, x, y;
int driver = DETECT, mode;
initgraph(&driver, &mode, "");
setfillstyle(1, 7);
bar(0, 0, getmaxx(), getmaxy());
WriteScores(0, 0);
for(key = 0; key < 9; key++)
box[key / 3][key % 3] = 0;
DrawBox();
int pl1 = 0, pl2 = 0;
char win, other;
ActivateMouse();
ShowMouse();
other = Menu();
do
{
while(1)
{
win = player(1);
if(win) break;
if(other == 1) win = computer();
else win = player(2);
if(win) break;
}
if(win == 1) pl1++;
else if(win == 2) pl2++;
WriteScores(pl1, pl2);
sound(534);
delay(500);
nosound();
if(getch() == 0) getch();
for(key = 0; key < 9; key++)
box[key / 3][key % 3] = 0;
DrawBox();
}
while(1);
}
Permissions in this forum:
Bạn không có quyền trả lời bài viết
|
|