Created
June 22, 2012 11:40
-
-
Save Iey4iej3/2972260 to your computer and use it in GitHub Desktop.
Source Code for Winner Game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
#define MAX_N 10 | |
#define MAX_MASK 1023 | |
int n; | |
int f[MAX_N+1][MAX_MASK+1][MAX_MASK+1]; | |
void calc(int k, int A, int B) | |
{ | |
for (int j=k+1; j<=n; j++) { | |
int tmp = 1<<(j-1); | |
if ((tmp&A)!=0 && f[j][B][A^tmp]==-1) { | |
f[k][A][B] = j; | |
return; | |
} | |
} | |
if (f[0][B][A] == -1) { | |
f[k][A][B] = 0; | |
} else if (f[0][B][A] == -2) { | |
f[k][A][B] = -2; | |
} else { | |
f[k][A][B] = -1; | |
} | |
} | |
void interaction() { | |
int np, nc, A, B, card, now; | |
printf("Computer first\n"); | |
printf("The number of your cards = "); | |
scanf("%d", &np); | |
printf("Input your cards\n"); | |
B = 0; | |
for (int k=0; k<np; k++) { | |
scanf("%d", &card); | |
B = B | (1 << (card-1)); | |
} | |
printf("The number of computer's cards = "); | |
scanf("%d", &nc); | |
printf("Input computer's cards\n"); | |
A = 0; | |
for (int k=0; k<nc; k++) { | |
scanf("%d", &card); | |
A = A | (1 << (card-1)); | |
} | |
printf("Last card (0 for new game) = "); | |
scanf("%d", &now); | |
while (A != 0) { | |
if (f[now][A][B] == -1) { | |
printf("Computer lose!\n"); | |
return; | |
} | |
printf("Computer put %d, your turn! (0 to pass)\n", f[now][A][B]); | |
now = f[now][A][B]; | |
A = A ^ (1 << (now-1)); | |
for (;;) { | |
scanf("%d", &card); | |
if (card == 0) { | |
now = 0; | |
break; | |
} | |
if (card > now && (B & (1 << (card-1))) != 0) { | |
now = card; | |
B = B ^ (1 << (card-1));; | |
break; | |
} | |
printf("Try again\n"); | |
} | |
} | |
printf("You lose!"); | |
} | |
int main() | |
{ | |
scanf("%d", &n); | |
int mask = (1<<n) - 1; | |
for (int A=1; A<=mask; A++) { | |
for (int k=0; k<=n; k++) { | |
f[k][A][0] = -1; | |
} | |
} | |
for (int A=1; A<=mask; A++) { | |
for (int B=1; B<=mask; B++) { | |
calc(0, A, B); | |
calc(0, B, A); | |
if (f[0][A][B] == -2) { | |
if (f[0][B][A] == -1) { | |
f[0][A][B] = 0; | |
} else { | |
f[0][A][B] = -1; | |
} | |
} | |
if (f[0][B][A] == -2) { | |
if (f[0][A][B] == -1) { | |
f[0][B][A] = 0; | |
} else { | |
f[0][B][A] = -1; | |
} | |
} | |
for (int k=1; k<=n; k++) { | |
calc(k, A, B); | |
calc(k, B, A); | |
} | |
} | |
} | |
interaction(); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment