Quantcast

Jump to content

» «
Photo

[REL][C++][AI][GAME] NIM

18 replies to this topic
Svip
  • Svip

    I eat babies

  • The Connection
  • Joined: 12 Nov 2001
  • None

#1

Posted 23 April 2005 - 10:51 PM Edited by Svip, 25 April 2005 - 03:59 PM.

I've made a game called NIM, it's a simple game, there are between 1 and 23 sticks on the table, each turn each player draws between 1 and 3 sticks.

The one to draw the last stick loses.

I've made a game that does the same in C++.

However, I made a teachable AI. It learns from every game you play with it.

You can play against another person, but that's not the beauty in this game.

Mirrors for the Win32 compiled version:
http://twozero.nextd...by.com/svip.rar - thanks to TwoZero.
http://www.theunderside.org/svip.rar - thanks to Waste.

Here is the code for you ( in case you want it, or want to compile it yourself, or you are not running Win32 ):
CODE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
int answer;
int sticks;
int turn;
int plst;
int i, j;
int knowelage[23][3] = { { 75, 50, 25 } , { 100, 75, 50 } , { 100,100, 75 } , { 100,100,100 } , { 100,100,100 } , { 100,100,100 } , { 100,100,100 } , { 100,100,100 } , { 100,100,100 } , { 100,100,100 } , { 100,100,100 } , { 100,100,100 } , { 100,100,100 } , { 100,100,100 } , { 100,100,100 } , { 100,100,100 } , { 100,100,100 } , { 100,100,100 } , { 100,100,100 } , { 100,100,100 } , { 100,100,100 } , { 100,100,100 } , { 100,100,100 } };
int drawn[23][2] = { { 5,5 } , { 5,5 } , { 5,5 } , { 5,5 } , { 5,5 } , { 5,5 } , { 5,5 } , { 5,5 } , { 5,5 } , { 5,5 } , { 5,5 } , { 5,5 } , { 5,5 } , { 5,5 } , { 5,5 } , { 5,5 } , { 5,5 } , { 5,5 } , { 5,5 } , { 5,5 } , { 5,5 } , { 5,5 } , { 5,5 } };
int line[2];
char leaveans [20];
do {
 answer = 0;
 printf("Select type of NIM game:\n( 1 Human vs. Human )\n( 2 Human vs. AI )\n");
 while ((answer > 2) || (answer < 1)) {
  scanf("%d",&answer);
 }
 if (answer == 1) {
  sticks = 0;
  while((sticks > 23) || (sticks < 1)) {
   printf("Enter amount out sticks ( 1 - 23 )\n");
   scanf("%d",&sticks);
  }
  printf("You have select %d sticks.\n",sticks);
  turn = 1;
  plst = 0;
  while(sticks > 0) {
   plst = 0;
   if(turn == 1) {
    while((plst > 3) || (plst < 1)) {
     printf("Player one, draw one to three sticks\n");  
     scanf("%d",&plst);
    }
    printf("Player one drew %d sticks.\n",plst);
    sticks = sticks - plst;
    printf("There are currently %d sticks left.\n",sticks);
    turn = 2;
   }
   else if(turn == 2) {
  for (i = 0; i <= 22; i++) {
   drawn[i][0] = 5;
   drawn[i][1] = 5;
  }
    while((plst > 3) || (plst < 1)) {
     printf("Player two, draw one to three sticks\n");
     scanf("%d",&plst);
    }
    printf("Player two drew %d sticks.\n",plst);
    sticks = sticks - plst;
    printf("There are currently %d sticks left.\n",sticks);
    turn = 1;
   }
  }
  if(sticks <= 0) {
   if(turn == 1) {
    printf("-----------------\nPlayer one won!\n-----------------\n");
   }
   else {
    printf("-----------------\nPlayer two won!\n-----------------\n");
   }
   printf("Wanna try again? ( yes or no )\n");
   scanf("%s",leaveans);
  }
 }
 else if (answer == 2) {
  sticks = 0;
  while((sticks > 23) || (sticks < 1)) {
   printf("Enter amount out sticks ( 1 - 23 )\n");
   scanf("%d",&sticks);
  }
  printf("You have select %d sticks.\n",sticks);
  turn = 1;
  plst = 0;
  while(sticks > 0) {
   plst = 0;
   if(turn == 1) {
    while((plst > 3) || (plst < 1)) {
     printf("Draw one to three sticks\n");  
     scanf("%d",&plst);
    }
    printf("You drew %d sticks.\n",plst);
    drawn[ sticks - 1][0] = plst - 1;
    sticks = sticks - plst;
    printf("There are currently %d sticks left.\n",sticks);
    turn = 2;
   }
   else if(turn == 2) {
    plst = 1;
    //If it knows better, meaning that it knows for knowelage that more sticks are betting to draw. It should take more.
    if ((knowelage[sticks - 1][1] > knowelage[sticks - 1][0]) and (sticks >= 2)) { plst = 2; }
    if ((knowelage[sticks - 1][2] > knowelage[sticks - 1][1]) and (sticks >= 3)) { plst = 3; }
    printf("The computer drew %d sticks.\n",plst);
    drawn[ sticks - 1][1] = plst - 1;
    sticks = sticks - plst;
    printf("There are currently %d sticks left.\n",sticks);
    turn = 1;
   }
  }
  if(sticks <= 0) {
   //In case I won, I should let the computer know that I did the right moves.
   if(turn == 1) {
    printf("-----------------\nYou won!\n-----------------\n");
    for(i = 0; i <= 22; i++ ) {
     //So if the draw was made, it should check it out.
     if(drawn[i][0] != 5) {
      //So, since I won, the move I made at this point in the game must have been good. Let's tell the computer that.
      knowelage[i][drawn[i][0]] += 1;
     }
     else if(drawn[i][1] != 5) {
      //Too bad for the computer it lost, so let us tell it that it's moved wasn't that good.
      knowelage[i][drawn[i][1]] -= 1;
     }
    }
   }
   else {
    printf("-----------------\nThe computer won!\n-----------------\n");
    //Now since the computer has won, then we should tell it that it did the right moves.
    for(i = 0; i <= 22; i++ ) {
     if(drawn[i][1] != 5) {
      //As it's move was good, then let us congrat it.
      knowelage[i][drawn[i][1]] += 1;
     }
     else if(drawn[i][0] != 5) {
      // :( My move was bad, but the computer wants to know too.
      knowelage[i][drawn[i][0]] -= 1;
     }
    }
   }
   printf("Wanna try again? ( yes or no ( \"knowtable\" for knowelage table of the AI ) )\n");
   scanf("%s",leaveans);
   if (strcmp(leaveans,"knowtable") == 0) {
    printf(" -| -1 | -2 | -3 \n");
    for(i = 0; i <= 22; i++) {
     printf("%d | %d | %d | %d\n",i + 1 ,knowelage[i][0],knowelage[i][1],knowelage[i][2]);
    }
   }
   else if (strcmp(leaveans,"drawntable") == 0) {
    printf(" -| Hm | AI \n");
    for (i = 0; i <= 22; i++) {
     line[0] = drawn[i][0] + 1;
     line[1] = drawn[i][1] + 1;
     if (drawn[i][0] == 5) {
      line[0] = 0;
     }
     if (drawn[i][1] == 5) {
      line[1] = 0;
     }
     printf("%d | %d | %d\n", i + 1, line[0], line[1]);
    }
   }
  }
 }
} while (strcmp(leaveans,"no") != 0);
return 0;
}


Note: there is no graphical in this game, as I don't believe in graphical programs.

I can't compile a Windows suitable binary file, so anyone with a compiler, could you compile it for the others?

Svip
  • Svip

    I eat babies

  • The Connection
  • Joined: 12 Nov 2001
  • None

#2

Posted 23 April 2005 - 11:05 PM Edited by Svip, 23 April 2005 - 11:20 PM.

NOTE! The code have been updated with some serious for the AI after this post was posted!

Please recopy your code if you haven't all ready!

The update insures that the AI gets correct data.

EDIT: Typing letters when it asks for numbers make it go into an infinity loop. So don't do that, kthx.

Yuck Fou
  • Yuck Fou

    <3 Mar

  • Members
  • Joined: 01 Feb 2005

#3

Posted 23 April 2005 - 11:22 PM Edited by Yuck Fou, 24 April 2005 - 12:11 AM.

I just played it, Svip.. and it's pretty nice. Seeing as how I'm a lazy impatient f*ck there's no way I can beat the AI with 23 sticks. tounge.gif Yay for pressing random buttons.

If you could give me a host, then I could post the compiled executable so people without compilers can actually play it.

Johnno
  • Johnno

    WD&P Ogre

  • Members
  • Joined: 15 Sep 2002

#4

Posted 24 April 2005 - 01:38 AM

Very nice. Excellent in fact. I'll be compiling and playing later today smile.gif

Waste
  • Waste

    Gangsta

  • Members
  • Joined: 15 Nov 2004

#5

Posted 24 April 2005 - 01:46 AM

QUOTE (Yuck Fou @ Apr 23 2005, 18:22)
If you could give me a host, then I could post the compiled executable so people without compilers can actually play it.

I'll host it.

Toadyd
  • Toadyd

  • Andolini Mafia Family
  • Joined: 10 Jan 2002
  • None

#6

Posted 24 April 2005 - 08:46 AM Edited by Toadyd, 24 April 2005 - 09:00 AM.

QUOTE (Waste @ Apr 24 2005, 02:46)
QUOTE (Yuck Fou @ Apr 23 2005, 18:22)
If you could give me a host, then I could post the compiled executable so people without compilers can actually play it.

I'll host it.

x2.

QUOTE
Yay for pressing random buttons.

Haha, this is pretty entertaining. Nice work Svip
tounge.gif

Svip
  • Svip

    I eat babies

  • The Connection
  • Joined: 12 Nov 2001
  • None

#7

Posted 24 April 2005 - 03:56 PM Edited by Svip, 24 April 2005 - 04:00 PM.

Thanks to TwoZero, the program is not compiled for you in Windows envourment, for all of you who uses Windows:

http://svip.porkbell...ploads/svip.exe

smile.gif

TwoZero
  • TwoZero

    Ghetto Star

  • The Connection
  • Joined: 15 Apr 2003

#8

Posted 24 April 2005 - 04:02 PM

And here's a mirror:
http://twozero.nextd...by.com/svip.rar.
Exe and source file.

Luke
  • Luke

    suckmyrocket

  • Inactive Staff
  • Joined: 01 Dec 2003
  • None

#9

Posted 24 April 2005 - 04:50 PM

TwoZero's Hosting > Svip's Hosting. Just look at the address. tounge.gif

So yea, It's nice Svip, I'll give it a try later on today, the only thing is: I had a maths teacher who used to play it, and he was damn good at it too, but he'd pay me if I won. Next version you make Svip, must pay me a pound if I win, then I'll be happy.

Waste
  • Waste

    Gangsta

  • Members
  • Joined: 15 Nov 2004

#10

Posted 24 April 2005 - 05:00 PM

It is a very nice game, Svip.
Thanks for sharing.

For the hell of it, another mirror: http://www.theunderside.org/svip.rar (same file as in TwoZero's posts.)

Svip
  • Svip

    I eat babies

  • The Connection
  • Joined: 12 Nov 2001
  • None

#11

Posted 25 April 2005 - 09:50 AM

TwoZero! Your application is not up to date. The computer keeps saving useless information, which will ruin the entire program and the AI.

Please copy the code from above which has been fixed.

NOTE: The mirrors by Me, TwoZero and Waste is useless as they are old versions of the application with a huge bug. Which means the AI doesn't work.

Waste
  • Waste

    Gangsta

  • Members
  • Joined: 15 Nov 2004

#12

Posted 25 April 2005 - 10:05 AM

Give me a newer version and I will update it.

Svip
  • Svip

    I eat babies

  • The Connection
  • Joined: 12 Nov 2001
  • None

#13

Posted 25 April 2005 - 01:50 PM

QUOTE (Waste @ Apr 25 2005, 12:05)
Give me a newer version and I will update it.

Sorry, I can't compile win32 binary files, only for Linux.

So in case you run a linux envorment client, you can do that.

But just for now. Please get someone to update it, it is the correct code above.

Octember
  • Octember

    propaganda inc

  • BUSTED!
  • Joined: 29 Oct 2004

#14

Posted 25 April 2005 - 02:20 PM

nice, i saw i when you first posted it on devart... kept on replacing the emotes...

finally i'll try it.

Waste
  • Waste

    Gangsta

  • Members
  • Joined: 15 Nov 2004

#15

Posted 25 April 2005 - 02:28 PM

QUOTE (Svip @ Apr 25 2005, 08:50)
QUOTE (Waste @ Apr 25 2005, 12:05)
Give me a newer version and I will update it.

Sorry, I can't compile win32 binary files, only for Linux.

So in case you run a linux envorment client, you can do that.

But just for now. Please get someone to update it, it is the correct code above.

Yes, I knew you couldn't.
I was just waiting for someone to compile it.

TwoZero
  • TwoZero

    Ghetto Star

  • The Connection
  • Joined: 15 Apr 2003

#16

Posted 25 April 2005 - 03:19 PM

Updated my mirror, there is something wrong with it though.. the computer keeps getting 1 stick, its not random between 1 and 3 (or maybe it should do that and I missed something).
I did have to change 2 lines of code for it to compile, both Borland and VC++ complained about those 2 lines:
CODE
    if ((knowelage[sticks - 1][1] > knowelage[sticks - 1][0]) and (sticks >= 2)) { plst = 2; }
   if ((knowelage[sticks - 1][2] > knowelage[sticks - 1][1]) and (sticks >= 3)) { plst = 3; }

Error in question:
CODE
Error E2377 e:\svip.cpp 94: If statement missing ) in function main()


So I changed those to lines to this:
CODE
   if (knowelage[sticks - 1][1] > knowelage[sticks - 1][0]) {
    if (sticks >= 2) {
     plst = 2;
 }
}
   if (knowelage[sticks - 1][2] > knowelage[sticks - 1][1]) {
    if (sticks >= 3) {
     plst = 3;
 }
}

Less effecient, but should do exactly the same I think..

Anyway.. mirror updated http://twozero.nextd...by.com/svip.rar.

Waste
  • Waste

    Gangsta

  • Members
  • Joined: 15 Nov 2004

#17

Posted 25 April 2005 - 03:56 PM

Mirror updated...

PresidentKiller
  • PresidentKiller

    Anger Manager

  • The Connection
  • Joined: 22 Dec 2004
  • Mexico

#18

Posted 26 April 2005 - 01:36 AM

QUOTE (TwoZero @ Apr 25 2005, 10:19)
[color=#EFCF00]Updated my mirror, there is something wrong with it though.. the computer keeps getting 1 stick, its not random between 1 and 3 (or maybe it should do that and I missed something).
I did have to change 2 lines of code for it to compile, both Borland and VC++ complained about those 2 lines:
CODE
    if ((knowelage[sticks - 1][1] > knowelage[sticks - 1][0]) and (sticks >= 2)) { plst = 2; }
   if ((knowelage[sticks - 1][2] > knowelage[sticks - 1][1]) and (sticks >= 3)) { plst = 3; }

Error in question:
CODE
Error E2377 e:\svip.cpp 94: If statement missing ) in function main()


So I changed those to lines to this:
CODE
   if (knowelage[sticks - 1][1] > knowelage[sticks - 1][0]) {
    if (sticks >= 2) {
     plst = 2;
 }
}
   if (knowelage[sticks - 1][2] > knowelage[sticks - 1][1]) {
    if (sticks >= 3) {
     plst = 3;
 }
}

You could just change the "and" for "&&"...

CODE
    if ((knowelage[sticks - 1][1] > knowelage[sticks - 1][0])&&(sticks >= 2)) { plst = 2; }
   if ((knowelage[sticks - 1][2] > knowelage[sticks - 1][1])&&(sticks >= 3)) { plst = 3; }


Nice work Svip. I'll give it a try. cookie.gif

TwoZero
  • TwoZero

    Ghetto Star

  • The Connection
  • Joined: 15 Apr 2003

#19

Posted 26 April 2005 - 05:36 AM

QUOTE (PresidentKiller @ Apr 26 2005, 02:36)
You could just change the "and" for "&&"...

That didn't work either for some reason..




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users