Jump to content

Recommended Posts

Posted

Hey guys, I recently moved into c++ from autoit and I'm working on a game called Nim. I haven't finished the logic for it quite yet, but I will worry about that later. When I try to compile what I have so far I get these errors:

1>------ Build started: Project: Nim2, Configuration: Debug Win32 ------
1>Compiling...
1>main.cpp
1>Linking...
1>main.obj : error LNK2019: unresolved external symbol "void __cdecl computerTurn(int,int * const,int)" (?computerTurn@@YAXHQAHH@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "void __cdecl playerTurn(int,int,int * const)" (?playerTurn@@YAXHHQAH@Z) referenced in function _main
1>E:\STUFF\My Code\C++\Projects\Nim2\Debug\Nim2.exe : fatal error LNK1120: 2 unresolved externals
1>Build log was saved at "file://e:\STUFF\My Code\C++\Projects\Nim2\Nim2\Debug\BuildLog.htm"
1>Nim2 - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

here is the code:

#include <iostream> //input and output
#include <time.h> //random seed

using namespace std;

void dispBoard(int, int[]);
void playerTurn(int, int , int[]);
void computerTurn(int, int[], int);
int winCheck(int[], int);
void results(int);

void main()
{
    int pilenum = 4;
    int pile[4] = {1, 3, 5, 7};

    srand(static_cast<int>(time(NULL))); //seed random number
    int playerID = rand() % 2 + 1; // generate random number (1 or 2)

    if(playerID == 1)
    {
        cout << "Player will go first." << endl;
        playerTurn(pilenum, playerID, pile);
    }
    else
    {
        cout << "Computer will go first." << endl;
    }

    while(winCheck(pile, pilenum) != 1){
        dispBoard(pilenum, pile);
        computerTurn(playerID, pile, pilenum);
        dispBoard(pilenum, pile);
        playerTurn(pilenum, playerID, pile);
    };

    results(playerID);
}

void dispBoard(int pilenum, int pile[])
{
    for(int x = 0; x < pilenum; x++)
    {
        for(int i = pile[x]; i > 0; i--)
        {
            cout << "*";
        }
        cout << endl;
    }
}

void playerTurn(int pilenum, int &playerID, int pile[])
{
    if(winCheck(pile, pilenum) != 0)
    {
        playerID = 1;

        int x = 0;
        int y = 0;

        cout << "Which pile? ";
        cin >> x;
        cout << "How many? ";
        cin >> y;

        if(x > 0 && x <= pilenum && y > 0 && y <= pile[x])
        {
            pile[x] -= y;
        }
    }
}

void computerTurn(int &playerID, int pile[], int pilenum)
{
    if(winCheck(pile, pilenum) != 0)
    {
        playerID = 2;

        int x = 0;
        int y = 0;

        while(1){
            srand(static_cast<int>(time(NULL))); //seed random number
            x = rand() % pilenum + 1;
            y = rand() % pile[x] + 1;
            if(y != 0)
            {
                break;
            }
        };

        pile[x] -= y;

        cout << "Computer took " << y << " from pile " << x << "." << endl;
    }
}

int winCheck(int pile[], int pilenum)
{
    int y = 0;

    for(int x = 0, y = 0; x < pilenum; x++)
    {
        y += pile[x];
    }

    return y;
}

void results(int playerID)
{
        if(playerID = 1)
        {
            cout << "YOU LOSE." << endl;
        }
        else
        {
            cout << "YOU WIN!!!" << endl;
        }
}

Any ideas?

Please don't clean the code unless it's neccesary to fix the problem. Thanks.

Posted (edited)

Here's the 'fixed' code...

This is going to come out harsher than I intend it, but I don't know shit about C++ and that was about a 10 second debug... so maybe you should back off of this and start reading through a good book starting with the preface. Anyone that has read the preface and/or the first chapter in a C++ book would know that 'int' and 'int&' are not the same. Also, I am of the firm opinion that command line linux is the best way to learn C++, not that windows ide crap... that kind of thing comes once you understand what is going on.

Just my opinion though.

#include <iostream> //input and output
#include <time.h> //random seed
#include <stdlib.h>

using namespace std;

void dispBoard(int, int[]);
void playerTurn(int, int&, int[]);
void computerTurn(int&, int[], int);
int winCheck(int[], int);
void results(int);

int main()
{
    int pilenum = 4;
    int pile[4] = {1, 3, 5, 7};

    srand(static_cast<int>(time(NULL))); //seed random number
    int playerID = rand() % 2 + 1; // generate random number (1 or 2)

    if(playerID == 1)
    {
        cout << "Player will go first." << endl;
        playerTurn(pilenum, playerID, pile);
    }
    else
    {
        cout << "Computer will go first." << endl;
    }

    while(winCheck(pile, pilenum) != 1){
        dispBoard(pilenum, pile);
        computerTurn(playerID, pile, pilenum);
        dispBoard(pilenum, pile);
        playerTurn(pilenum, playerID, pile);
    };

    results(playerID);
    return 0;
}

void dispBoard(int pilenum, int pile[])
{
    for(int x = 0; x < pilenum; x++)
    {
        for(int i = pile[x]; i > 0; i--)
        {
            cout << "*";
        }
        cout << endl;
    }
}

void playerTurn(int pilenum, int &playerID, int pile[])
{
    if(winCheck(pile, pilenum) != 0)
    {
        playerID = 1;

        int x = 0;
        int y = 0;

        cout << "Which pile? ";
        cin >> x;
        cout << "How many? ";
        cin >> y;

        if(x > 0 && x <= pilenum && y > 0 && y <= pile[x])
        {
            pile[x] -= y;
        }
    }
}

void computerTurn(int &playerID, int pile[], int pilenum)
{
    if(winCheck(pile, pilenum) != 0)
    {
        playerID = 2;

        int x = 0;
        int y = 0;

        while(1){
            srand(static_cast<int>(time(NULL))); //seed random number
            x = rand() % pilenum + 1;
            y = rand() % pile[x] + 1;
            if(y != 0)
            {
                break;
            }
        };

        pile[x] -= y;

        cout << "Computer took " << y << " from pile " << x << "." << endl;
    }
}

int winCheck(int pile[], int pilenum)
{
    int y = 0;

    for(int x = 0, y = 0; x < pilenum; x++)
    {
        y += pile[x];
    }

    return y;
}

void results(int playerID)
{
        if(playerID = 1)
        {
            cout << "YOU LOSE." << endl;
        }
        else
        {
            cout << "YOU WIN!!!" << endl;
        }
}

EDIT:

Alright, to not be a total jackass I would point out that your code shows that you are a pretty intelligent person... but one that has some large and fundamental holes in their knowledge.

Edited by Wus
Posted

EDIT:

Alright, to not be a total jackass I would point out that your code shows that you are a pretty intelligent person... but one that has some large and fundamental holes in their knowledge.

That's an understatement given that the user is passing arrays by value!
Posted

I didn't think C++ supported arrays by value. I figured that they were always treated as arrays even using the array bracket declarator.

Hmm, I gues this is true. Surprisingly C++ completely prevents you from shooting yourself in this one particular case. I have never used the bracket notation, nor do I ever intend to. I was under the impression that it allowed you to do something stupid but apparently not.

Apparently today is the day of C++ learning for me.

Posted

Here's the 'fixed' code...

This is going to come out harsher than I intend it, but I don't know shit about C++ and that was about a 10 second debug... so maybe you should back off of this and start reading through a good book starting with the preface. Anyone that has read the preface and/or the first chapter in a C++ book would know that 'int' and 'int&' are not the same. Also, I am of the firm opinion that command line linux is the best way to learn C++, not that windows ide crap... that kind of thing comes once you understand what is going on.

Just my opinion though.

EDIT:

Alright, to not be a total jackass I would point out that your code shows that you are a pretty intelligent person... but one that has some large and fundamental holes in their knowledge.

Thanks guys, I was unaware that I had to show passing by reference in the function's prototype. Oh, and I do use Ubuntu as my main OS, and am trying to code more using the Eclipse IDE in linux but I'm in a C++ class at my highschool that uses VC++ so for now it will be easier to just use that.
Posted

Really vim? Pah! Real programmers use ed.

Posted

As to Editors... I generally try to use Code::Blocks. Especially since they recently came out with a stable version that works well, and I'm not constantly having to download the latest nightly to fix some issue.

I must say that I do have a great admiration for Eclipse though. It has helped me a lot with several editing features, and as much as I'm not a big Java fan, I am rather finding myself liking Eclipse more and more. If I could have some more of it's functionality in Code::Blocks, then I would probably stick harder to Code::Blocks.

Thanks,

Jarvis

AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Posted

Code::Blocks is open source isn't it? Or at least uses plugins I know that much. Write what's missing yourself.

It is both open source and has a plugin architecture. If I had the time, I would write a plugin myself. However, I don't have the time at the moment to do that. I do have the time to download Eclipse though :).

Thanks though,

Jarvis

AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Posted

I love Eclipse. It took me a long while to warm to it, but now that I have started to figure out what a lot of the bells and whistles do I use it pretty exclusively. I need to qualify that by saying that I code almost exclusively in Java these days. And having said that, one of my friends swears by CDT (essentially just the C++ version of Eclipse), though I have not used it myself. I think the reason it took me a while to warm to it initially was the learning curve was not worth it for the pathetically simple stuff I used to do... I mean, if you only need 2 freaking class files, then Eclipse is probably more effort than it is worth. But once I started managing a non-trivial project I started using all the fancy features and plug-ins.

By the way though, as far as command line editors go... Emacs is the only one for me.

Posted

Well, that makes a difference then. I dislike java and don't write in it.

I dislike Java for the most part and also don't write in it, but using CDT (C++), and PDT (PHP), it has definitely won me over very quickly. I have also installed the plugins for Python, Perl, and XML so I can do quite a lot with one spot.

Thanks,

Jarvis

AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...