Quantcast
Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
    1. Welcome to GTAForums!

    1. GTANet.com

    2. GTANet 20th Anniversary

    1. GTA Online

      1. The Cayo Perico Heist
      2. Find Lobbies & Players
      3. Guides & Strategies
      4. Vehicles
      5. Content Creator
      6. Help & Support
    2. Red Dead Online

      1. Frontier Pursuits
      2. Find Lobbies & Outlaws
      3. Help & Support
    3. Crews

    1. Red Dead Redemption 2

      1. PC
      2. Help & Support
    2. Red Dead Redemption

    1. Grand Theft Auto Series

      1. St. Andrews Cathedral
    2. GTA VI

    3. GTA V

      1. Guides & Strategies
      2. Help & Support
    4. GTA IV

      1. The Lost and Damned
      2. The Ballad of Gay Tony
      3. Guides & Strategies
      4. Help & Support
    5. GTA San Andreas

      1. Guides & Strategies
      2. Help & Support
    6. GTA Vice City

      1. Guides & Strategies
      2. Help & Support
    7. GTA III

      1. Guides & Strategies
      2. Help & Support
    8. Portable Games

      1. GTA Chinatown Wars
      2. GTA Vice City Stories
      3. GTA Liberty City Stories
    9. Top-Down Games

      1. GTA Advance
      2. GTA 2
      3. GTA
    1. GTA Mods

      1. GTA V
      2. GTA IV
      3. GTA III, VC & SA
      4. Tutorials
    2. Red Dead Mods

      1. Documentation
    3. Mod Showroom

      1. Scripts & Plugins
      2. Maps
      3. Total Conversions
      4. Vehicles
      5. Textures
      6. Characters
      7. Tools
      8. Other
      9. Workshop
    4. Featured Mods

      1. Design Your Own Mission
      2. OpenIV
      3. GTA: Underground
      4. GTA: Liberty City
      5. GTA: State of Liberty
    1. Rockstar Games

    2. Rockstar Collectors

    1. Off-Topic

      1. General Chat
      2. Gaming
      3. Technology
      4. Movies & TV
      5. Music
      6. Sports
      7. Vehicles
    2. Expression

      1. Graphics / Visual Arts
      2. GFX Requests & Tutorials
      3. Writers' Discussion
      4. Debates & Discussion
    1. Announcements

    2. Support

      1. Court House
    3. Suggestions

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


Svip

Recommended Posts

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.nextdoorcerby.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 ):

 

#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?

Edited by Svip
Link to post
Share on other sites

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.

Edited by Svip
Link to post
Share on other sites

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.

Edited by Yuck Fou
Link to post
Share on other sites

 

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.

 

 

Yay for pressing random buttons.

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

Edited by Toadyd
Link to post
Share on other sites

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.

Link to post
Share on other sites

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.

Link to post
Share on other sites
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.

Link to post
Share on other sites
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.

Link to post
Share on other sites

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:

 

     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:

 

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

 

 

So I changed those to lines to this:

 

    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.nextdoorcerby.com/svip.rar.

Link to post
Share on other sites

 

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:

 

     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:

 

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

 

 

So I changed those to lines to this:

 

    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 "&&"...

 

 

     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

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • 1 User Currently Viewing
    0 members, 0 Anonymous, 1 Guest

×
×
  • Create New...

Important Information

By using GTAForums.com, you agree to our Terms of Use and Privacy Policy.