4 Replies - 1098 Views - Last Post: 30 November 2011 - 03:36 PM Rate Topic: -----

#1 Painteagle   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 30-November 11

Battleship AI unknown error

Posted 30 November 2011 - 12:47 PM

I wrote this Battleship AI during a particularly boring class and after I fixed all the bugs I could find I kept getting errors. I'm ignoring the warnings about changing float to int, but the other things I don't understand. Here is what I have so far:
//battleship
#include<iostream>
#include<math.h>
#include<time.h>
#include<stdio.h>
#include <windows.h>
using namespace std;

double grid[10][10];
int q1=0;
int q2=0;
int i1=0;
int i2=0;
int q12=0;
int q22=0;
int cntc=0;
int cntp=0;
float c;
char qst='m',p1,ip1,plgn;

   
int guess()
{
    time_t seconds;
    seconds = time (NULL);
    float c = seconds;
    q1 = cos(c+7) * cos(c+7) * 10;
    q2 = sin(c+3) * sin(c+3) * 10;
    if (q1==0)p1='A';
    if (q1==1)p1='B';
    if (q1==2)p1='C';
    if (q1==3)p1='D';
    if (q1==4)p1='E';
    if (q1==5)p1='F';
    if (q1==6)p1='G';
    if (q1==7)p1='H';
    if (q1==8)p1='I';
    if (q1==9)p1='J';
    if(grid[q1][q2]==0)
    {
        cout<<p1<<","<<q2+1<<endl;
    }    
}    

int right(qst)
{
    if (qst=='h'||qst=='H')
    {
        grid[q1][q2]=1;
        cntc=cntc+1;
        return (1);
    }
    else if (qst=='m'||qst=='M')
    {
        grig[q1][q2]=2;
        return (0)
    }    
    else
    {
        cout << "What? "<<endl;
        cin >> qst;
        right (qst);
    }
}    

int strat()
{
    for (int lcv = 0, lcv<4, lcv++)
    {
        if (lcv = 0)
        {
            q12=q1+1;
            q22=q2+1
        }
        else if (lcv = 1)
        {
            q12=q1-1;
            q22=q2+1
        }   
        else if (lcv = 2)
        {
            q12=q1+1;
            q22=q2-1
        }   
        else if (lcv = 3)
        {
            q12=q1-1;
            q22=q2-1
        }   
        if(q12<0||q12>9||q22<0||q22>9||grid[q12][q22]);
        else
        {
            if (q12==0)p1='A';
            if (q12==1)p1='B';
            if (q12==2)p1='C';
            if (q12==3)p1='D';
            if (q12==4)p1='E';
            if (q12==5)p1='F';
            if (q12==6)p1='G';
            if (q12==7)p1='H';
            if (q12==8)p1='I';
            if (q12==9)p1='J';
            cout<<p1<<","<<q22+1;
            cin>>qst;
            if(right(qst))
            {
                q1=q12;
                q2=q22;
                strat();
                break;
            }    
        }
    }
}            

main()
{
    int cgrid[10][10];
    for (int lcvq=0,lcvq<10,lcvq++)
    {
        for (int lcvw=0, lcvw<10,lcvw++)
        {
            grid[lcvq][lcvw]=0;
            cgrid[lcvq][lcvw]=0;
        }
    }        
    cout<<"Set up your board."<endl;
    Sleep(4000);
    cgrid[0][2]=1;
    cgrid[0][3]=1;
    cgrid[2][1]=1;
    cgrid[2][2]=1;
    cgrid[2][3]=1;
    cgrid[2][6]=1;
    cgrid[3][6]=1;
    cgrid[4][6]=1;
    cgrid[5][6]=1;
    cgrid[5][8]=1;
    cgrid[6][8]=1;
    cgrid[7][8]=1;
    cgrid[8][1]=1;
    cgrid[8][2]=1;
    cgrid[8][3]=1;
    cgrid[8][4]=1;
    cgrid[8][5]=1;
    while(1==1)
    {
        int lcv;
        float lcv2 = 1;
        if (lcv%2==0)
        {
            cout<<"Your turn"<<endl;
            cin>>ip1>>i2;
            if(ip1=='A'||ip1=='a')i1=0;
            if(ip1=='B'||ip1=='b')i1=1;
            if(ip1=='C'||ip1=='c')i1=2;
            if(ip1=='D'||ip1=='d')i1=3;
            if(ip1=='E'||ip1=='e')i1=4;
            if(ip1=='F'||ip1=='f')i1=5;
            if(ip1=='G'||ip1=='g')i1=6;
            if(ip1=='H'||ip1=='h')i1=7;
            if(ip1=='I'||ip1=='i')i1=8;
            if(ip1=='J'||ip1=='j')i1=9;
            if (cgrid[i1][i2]==2)cout<<"You already tried there"<<endl;
            if (cgrid[i1][i1]==1)
            {
                cout<<"Hit!"<<endl;
                cntp++
            }
            if (cgrid==0) cout<<"Miss"<<endl;
            cgrid[i1][i2]=2
        }
        else
        {
            if (right(qst))
            {
                strat()
            }
            else guess()
        }
        if (cntp==17)
        {
            cout <<"You win!!!!"<<endl<<"Play again?(y/n)"<<endl;
            cin>>plgn;
            if (plgn!='y'||plgn!='Y')
            cout<<"Bye";
            Sleep(2000);
            exit(1);
        }
        lcv++;
    }
}


:code:

This post has been edited by jimblumberg: 30 November 2011 - 12:53 PM
Reason for edit:: Added missing Code Tags, Please learn to use them.


Is This A Good Question/Topic? 0
  • +

Replies To: Battleship AI unknown error

#2 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Battleship AI unknown error

Posted 30 November 2011 - 12:55 PM

And your errors are? Post the complete error messages exactly as they appear in your development environment. Also you should never "ignore" warnings, fixing them now will prevent headaches later. You should also stop using global variables. They will make your program harder to understand and troubleshoot.


Jim

This post has been edited by jimblumberg: 30 November 2011 - 12:56 PM

Was This Post Helpful? 1
  • +
  • -

#3 Painteagle   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 30-November 11

Re: Battleship AI unknown error

Posted 30 November 2011 - 03:05 PM

Ok so let me break it down. I've done some more work on this to make it run a little better. Here is the code for the function that causes the most trouble:
int strat()//this checks the four corosponding places not asking 
{          //about shots that have been fired already or ones that
    for (int lcv = 0, lcv<4, lcv++)//go out of bounds
    {
        if (lcv = 0)
        {
            q12=q1+1;
            q22=q2+1;
        }
        else if (lcv = 1)
        {
            q12=q1-1;
            q22=q2+1;
        }   
        else if (lcv = 2)
        {
            q12=q1+1;
            q22=q2-1;
        }   
        else if (lcv = 3)
        {
            q12=q1-1;
            q22=q2-1;
        }   
        if(q12<0||q12>9||q22<0||q22>9||grid[q12][q22]);
        else
        {
            if (q12==0)p1='A';
            if (q12==1)p1='B';
            if (q12==2)p1='C';
            if (q12==3)p1='D';
            if (q12==4)p1='E';
            if (q12==5)p1='F';
            if (q12==6)p1='G';
            if (q12==7)p1='H';
            if (q12==8)p1='I';
            if (q12==9)p1='J';
            cout<<p1<<","<<q22+1;
            cin>>qst;
            if(right(qst))
            {
                q1=q12;
                q2=q22;//I would like to have a way to get into the 
                break;//middle of this function where I left off before
            }    
            else break;
        }
    }
}            

Here are my errors:
line 3 "syntax error before')' token"
line 9 "syntax error before '}' token"
line 15 "syntax error before 'else'"
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Battleship AI unknown error

Posted 30 November 2011 - 03:14 PM

Your first error in line #3:
for (int lcv = 0, lcv<4, lcv++)

You have used commas instead of semicolons to separate the different parts of your for loop.

For the other errors post the complete program. Also you seem to be using the assignment operator= in you if statements, instead of the comparison operator==. For example the following line is incorrect:
if (lcv = 0)


Jim
Was This Post Helpful? 0
  • +
  • -

#5 Painteagle   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 30-November 11

Re: Battleship AI unknown error

Posted 30 November 2011 - 03:36 PM

Thanks. That fixed it. It has been a while since I've been at my compiler.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1