6 Replies - 1030 Views - Last Post: 10 December 2014 - 05:59 AM Rate Topic: -----

#1 taylorabc101   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-December 14

I have been trying to learn how to make AI in C++ so I made a game.

Posted 08 December 2014 - 06:21 AM

#include <iostream>
#include "windows.h"
#include <ctime>

using namespace std;
char map[100][100];
char map1[100][100] = {
    "#########################",
    "#@            #         #",
    "#             #    !    #",
    "#             #         #",
    "#                *      #",
    "#########################",
    "#    #     #   ######## #",
    "#### # ### # ###        #",
    "#    #   # ###   ### ####",
    "# ###### #     ### # ## #",
    "#        # #####   # #  #",
    "###### #       # ### # ##",
    "#      # ####### #      #",
    "# ###### #       ###### #",
    "#        ### ###     *# #",
    "##########     ######## #",
    "#                       #",
    "#########################"
};
char enemymap1[100][100] = {
    "#########################",
    "#@            #         #",
    "#             #    !    #",
    "#             #         #",
    "#                *      #",
    "#########################",
    "#    #     #   ######## #",
    "#### # ### # ###        #",
    "#    #   # ###   ### ####",
    "# ###### #     ### # ## #",
    "#        # #####   # #  #",
    "###### #       # ### # ##",
    "#      # ####### #      #",
    "# ###### #       ###### #",
    "#        ### ###     *# #",
    "##########     ######## #",
    "#                       #",
    "#########################"
};



char map2[100][100] = {
    "#########################",
    "#@#      # # # # ## #   #",
    "# #### # #   ! #    ### #",
    "#      # ##### ####   # #",
    "##### ##   #   #      # #",
    "#   # ## # ### # ## # # #",
    "# # # ## # #      # # # #",
    "# #   #  # ### ## #######",
    "# # # ## #     #  # #   #",
    "##### ## ##### # ## ### #",
    "#     ##       #  #     #",
    "############  ##### ### #",
    "#          #            #",
    "# ###### ##### ######## #",
    "#            #        # #",
    "# ## ######### # ###### #",
    "#  # #         # #*     #",
    "#########################"
};
char enemymap2[100][100] = {
    "#########################",
    "#@#      # # # # ## #   #",
    "# #### # #   ! #    ### #",
    "#      # ##### ####   # #",
    "##### ##   #   #      # #",
    "#   # ## # ### # ## # # #",
    "# # # ## # #      # # # #",
    "# #   #  # ### ## #######",
    "# # # ## #     #  # #   #",
    "##### ## ##### # ## ### #",
    "#     ##       #  #     #",
    "############  ##### ### #",
    "#          #            #",
    "# ###### ##### ######## #",
    "#            #        # #",
    "# ## ######### # ###### #",
    "#  # #         # #*     #",
    "#########################"
};

char map3[100][100] = {
    "#########################",
    "#           #           #",
    "######## ####### ########",
    "#                       #",
    "# #### ##### #####      #",
    "# #    #         #      #",
    "# #### # # ##### # #### #",
    "#    # # # #     # #    #",
    "# ## ### # # ### ### ## #",
    "# *#     #  @  #     #  #",
    "# ## ### ### # # ### ## #",
    "#    # #     # # # #    #",
    "###### # ##### # # ######",
    "## # # #         # # # ##",
    "#  # # ##### ##### # #  #",
    "## # # # # # # # # # # ##",
    "#                       #",
    "#########################"
};

char map4[100][100] = {
    "#########################",
    "#                       #",
    "#                       #",
    "#         ## ##         #",
    "#        # # # #        #",
    "#        #     #        #",
    "#         # # #         #",
    "#         # # #         #",
    "#         # # #         #",
    "#         # # #         #",
    "#         # # #         #",
    "#     ####  #  ####     #",
    "#    #      #      #    #",
    "#    # #         # #    #",
    "#    #   ### ###   #    #",
    "#     ###  # #  ###     #",
    "#       #       #       #",
    "#########################"
};

int level = 1;
int x;
int y;
int playerx = 1;
int playery = 1;
int enemyx = 3;
int enemyy = 3;

boolean enemyCanMove = true;

bool game_running = true;

int main()
{   game_running = true;
    bool init_running = true;
    while(init_running){
    game_running = true;
    if(level==1){
        for(int y=0;y<100;y++){
            for(int x=0;x<100;x++){
                map[x][y] = map1[x][y];
            }
        }
    } else if(level==2) {
        for(int y=0;y<100;y++){
            for(int x=0;x<100;x++){
                map[x][y] = map2[x][y];
            }
        }
    } else if(level==3) {
        for(int y=0;y<100;y++){
            for(int x=0;x<100;x++){
                map[x][y] = map3[x][y];
            }
        }
    } else if(level==4) {
        init_running = false;
        game_running = false;
    }
    for(int j=0;j<18;j++){
        for(int i=0;i<25;i++){
            if(map[i][j] == '@'){
                x = j;
                y = i;
                break;
            }
        }
    }

    for(int k=0;k<18;k++){ //scan for enemy
        for(int l=0;l<25;l++){
            if(map[l][k] == '!'){
                enemyx = k;
                enemyy = l;
                break;
            }
        }
    }
    while(game_running == true){
        system("cls");
        for(int display=0; display<18; display++){
            cout << map[display] << endl;

        }
cout << playerx << endl;
    cout << playery << endl;
    cout << enemyx << endl;
    cout << enemyy << endl;
        system("pause>nul");

        if(GetAsyncKeyState(VK_DOWN)){
            int y2 = y+1;
            if(map[y2][x] == ' '){
                map[y][x] = ' ';
                y++;
                map[y][x] = '@';
                playerx = x;
                playery = y;
            }
            else if(map[y2][x] == '*') {
                    game_running = false;
                    level++;
                    playerx = 1;
                    playery = 1;
                }
                else if (map[y2][x] == '!'){ // if player hits enemy
                   game_running = false;
                   init_running = false;
                }
        }

        if(GetAsyncKeyState(VK_UP)){
            int y2 = y-1;
            if(map[y2][x] == ' '){
                map[y][x] = ' ';
                y--;
                map[y][x] = '@';
                playerx = x;
                playery = y;
            }
            else if(map[y2][x] == '*') {
                    game_running = false;
                   level++;
                   playerx = 1;
                    playery = 1;
                }
                else if (map[y2][x] == '!'){ // if player hits enemy
                   game_running = false;
                   init_running = false;
                }
        }

        if(GetAsyncKeyState(VK_RIGHT)){
            int x2 = x+1;
            if(map[y][x2] == ' '){
                map[y][x] = ' ';
                x++;
                map[y][x] = '@';
                playerx = x;
                playery = y;
            }
            else if(map[y][x2] == '*') {
                    game_running = false;
                    level++;
                    playerx = 1;
                    playery = 1;
                }
                 else if (map[y][x2] == '!'){ // if player hits enemy
                   game_running = false;
                   init_running = false;
                }
        }

        if(GetAsyncKeyState(VK_LEFT)){
            int x2 = x-1;
            if(map[y][x2] == ' '){
                map[y][x] = ' ';
                x--;
                map[y][x] = '@';
                playerx = x;
                playery = y;
            }
            else if(map[y][x2] == '*') {
                    game_running = false;
                    level++;
                    playerx = 1;
                    playery = 1;
                }
                else if (map[y][x2] == '!'){ // if player hits enemy
                   game_running = false;
                   init_running = false;
                }
        }

        if(GetAsyncKeyState(VK_ESCAPE)){
            game_running = false;
        init_running = false;

        }
        //enemy
        //restructure for better performance
        /*
        1.if boolean enemyCanMove is true (done)
        2.check player location or check enemy location !Update: scan for walls and old paths first using an "OR".
     2.25 How to make an array to track where the AI has been.
      2.5 update locations
        3.check for walls or check for space, idk yet
        4.check for victory or key
        */
        if (GetAsyncKeyState(VK_DOWN)|| GetAsyncKeyState(VK_UP)||GetAsyncKeyState(VK_LEFT)||GetAsyncKeyState(VK_RIGHT)){
            if (enemyCanMove){
               /* 1 left  */ if (enemymap1[enemyy][enemyx+1] == '^' ){
               /* move up */  if(playery < enemyy){
                        /* Update player map */ map[enemyy-1][enemyx] = '!';
                        /* remove old enemy location */ map[enemyy][enemyx] = ' ';
                        /* move enemy location */enemymap1[enemyy-1][enemyx] = '!';
                        /* Update enemy map */  enemymap1[enemyy][enemyx] = '^';
                        }
                   /* move down */  else if(playery > enemyy){
                        /* Update player map */ map[enemyy+1][enemyx] = '!';
                        /* remove old enemy location */ map[enemyy][enemyx] = ' ';
                        /* Update enemy map */  enemymap1[enemyy][enemyx] = '^';
                        /* move enemy location */enemymap1[enemyy+1][enemyx] = '!';
                        }
					/* move left */	else if (playerx < enemyx){
                        /* Update player map */ map[enemyy][enemyx-1] = '!';
                        /* remove old enemy location */ map[enemyy][enemyx] = ' ';
                        /* Update enemy map */  enemymap1[enemyy][enemyx] = '^';
                        /* move enemy location */enemymap1[enemyy][enemyx-1] = '!';
                        }
							}
               /* 2 right*/   else if (enemymap1[enemyy][enemyx-1] == '^' ) {
                         /* move up */  if(playery < enemyy){
                        /* Update player map */ map[enemyy-1][enemyx] = '!';
                        /* remove old enemy location */ map[enemyy][enemyx] = ' ';
                        /* Update enemy map */  enemymap1[enemyy][enemyx] = '^';
                        /* move enemy location */enemymap1[enemyy-1][enemyx] = '!';
                        }
                   /* move down */  else if(playery > enemyy){
                        /* Update player map */ map[enemyy+1][enemyx] = '!';
                        /* remove old enemy location */ map[enemyy][enemyx] = ' ';
                        /* Update enemy map */  enemymap1[enemyy][enemyx] = '^';
                        /* move enemy location */enemymap1[enemyy+1][enemyx] = '!';
                        }
					/* move right */	else if (playerx > enemyx){
                        /* Update player map */ map[enemyy][enemyx+1] = '!';
                        /* remove old enemy location */ map[enemyy][enemyx] = ' ';
                        /* Update enemy map */  enemymap1[enemyy][enemyx] = '^';
                        /* move enemy location */enemymap1[enemyy][enemyx+1] = '!';
                        }
                        }
               /* 3 down*/  else if (enemymap1[enemyy+1][enemyx] == '^' ){
                        /* move up */  if(playery < enemyy){
                        /* Update player map */ map[enemyy-1][enemyx] = '!';
                        /* remove old enemy location */ map[enemyy][enemyx] = ' ';
                        /* Update enemy map */  enemymap1[enemyy][enemyx] = '^';
                        /* move enemy location */enemymap1[enemyy-1][enemyx] = '!';
                        }
					/* move right */	else if (playerx > enemyx){
                        /* Update player map */ map[enemyy][enemyx+1] = '!';
                        /* remove old enemy location */ map[enemyy][enemyx] = ' ';
                        /* Update enemy map */  enemymap1[enemyy][enemyx] = '^';
                        /* move enemy location */enemymap1[enemyy][enemyx+1] = '!';
                        }
                    /* move left */	else if (playerx < enemyx){
                        /* Update player map */ map[enemyy][enemyx-1] = '!';
                        /* remove old enemy location */ map[enemyy][enemyx] = ' ';
                        /* Update enemy map */  enemymap1[enemyy][enemyx] = '^';
                        /* move enemy location */enemymap1[enemyy][enemyx-1] = '!';
                        }
                        }
               /* 4 up */  else if (enemymap1[enemyy-1][enemyx] == '^' ){
                        /* move left */	 if (playerx < enemyx){
                        /* Update player map */ map[enemyy][enemyx-1] = '!';
                        /* remove old enemy location */ map[enemyy][enemyx] = ' ';
                        /* Update enemy map */  enemymap1[enemyy][enemyx] = '^';
                        /* move enemy location */enemymap1[enemyy][enemyx-1] = '!';
                        }
                        /* move right */	else if (playerx > enemyx){
                        /* Update player map */ map[enemyy][enemyx+1] = '!';
                        /* remove old enemy location */ map[enemyy][enemyx] = ' ';
                        /* Update enemy map */  enemymap1[enemyy][enemyx] = '^';
                        /* move enemy location */enemymap1[enemyy][enemyx+1] = '!';
                        }
                        /* move down */  else if(playery > enemyy){
                        /* Update player map */ map[enemyy+1][enemyx] = '!';
                        /* remove old enemy location */ map[enemyy][enemyx] = ' ';
                        /* Update enemy map */  enemymap1[enemyy][enemyx] = '^';
                        /* move enemy location */enemymap1[enemyy+1][enemyx] = '!';
                        }
                        }
               /* 5 */  else {
                        if (playerx > enemyx){
                        if (map[enemyy][enemyx+1] == ' '){
                        map[enemyy][enemyx] = ' ';
                        enemyx = enemyx + 1;
                        map[enemyy][enemyx] = '!';
                        }
                        }
                        else if(playerx < enemyx){
                        if (map[enemyy][enemyx-1] == ' '){
                        map[enemyy][enemyx] = ' ';
                        enemyx = enemyx - 1;
                        map[enemyy][enemyx] = '!';
                        }
                        }
                        else if(playery > enemyy){
                        if (map[enemyy+1][enemyx] == ' '){
                        map[enemyy][enemyx] = ' ';
                        enemyy = enemyy + 1;
                        map[enemyy][enemyx] = '!';
                        }
                        }
                        else if(playery < enemyy){
                        if (map[enemyy-1][enemyx] == ' '){
                        map[enemyy][enemyx] = ' ';
                        enemyy = enemyy - 1;
                        map[enemyy][enemyx] = '!';
                        }
                        }
                        }






              /*  if (playerx > enemyx){
                   if (map[enemyy][enemyx+1] == ' '){
                    map[enemyy][enemyx] = ' ';
                    enemyx = enemyx + 1;
                   map[enemyy][enemyx] = '!';
                   }
                }
                else if(playerx < enemyx){
                if (map[enemyy][enemyx-1] == ' '){
                    map[enemyy][enemyx] = ' ';
                 enemyx = enemyx - 1;
                 map[enemyy][enemyx] = '!';
                }
                }
                else if(playery > enemyy){
                if (map[enemyy+1][enemyx] == ' '){
                    map[enemyy][enemyx] = ' ';
                 enemyy = enemyy + 1;
                 map[enemyy][enemyx] = '!';
                }
            }
            else if(playery < enemyy){
            if (map[enemyy-1][enemyx] == ' '){
                map[enemyy][enemyx] = ' ';
                 enemyy = enemyy - 1;
                 map[enemyy][enemyx] = '!';
            }
        }
        */
            }
            }
    }
    }
    system("cls");
    cout << "GAME OVER" << endl;
    cout << playerx << endl;
    cout << playery << endl;
    cout << enemyx << endl;
    cout << enemyy << endl;

    return 0;
}


if you run this game the ai is broken. it will hit a wall and break, I was wondering if someone could point me towards a way of learning how to make an AI that can path threw a maze to a player.

This post has been edited by modi123_1: 08 December 2014 - 08:11 AM
Reason for edit:: please use the 'code' button in the editor


Is This A Good Question/Topic? 0
  • +

Replies To: I have been trying to learn how to make AI in C++ so I made a game.

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14929
  • View blog
  • Posts: 59,622
  • Joined: 12-June 08

Re: I have been trying to learn how to make AI in C++ so I made a game.

Posted 08 December 2014 - 08:14 AM

Quote

if you run this game the ai is broken. it will hit a wall and break,

Explain this. What do you mean by "and break"?

Perhaps, around line 303, you can add an option to check for a wall. I am assuming a wall is the #, right?
Was This Post Helpful? 0
  • +
  • -

#3 BBeck   User is offline

  • Here to help.
  • member icon


Reputation: 792
  • View blog
  • Posts: 1,886
  • Joined: 24-April 12

Re: I have been trying to learn how to make AI in C++ so I made a game.

Posted 08 December 2014 - 09:37 PM

AI is actually one of my weaknesses. But are you familiar with A*?
Was This Post Helpful? 1
  • +
  • -

#4 taylorabc101   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-December 14

Re: I have been trying to learn how to make AI in C++ so I made a game.

Posted 09 December 2014 - 03:32 AM

View PostBBeck, on 08 December 2014 - 09:37 PM, said:

AI is actually one of my weaknesses. But are you familiar with A*?

Hey thanks for the fast response. I did not know anything about A* or depth first search. I'm going to do some research and learn one of them to the fullest of my ability, if i need more help i will update and fix my post, sorry for the inconvenience.
Was This Post Helpful? 0
  • +
  • -

#5 BBeck   User is offline

  • Here to help.
  • member icon


Reputation: 792
  • View blog
  • Posts: 1,886
  • Joined: 24-April 12

Re: I have been trying to learn how to make AI in C++ so I made a game.

Posted 09 December 2014 - 04:41 AM

No. No problem. That's what we're here for. With a lot of this stuff its difficult to know what questions to ask. Without someone to kind of point the way, how do you even know where to look?

I've spent a lot of days just trying to figure out what questions I should be asking. The big one that comes to mind is Axis Aligned Bounding Boxes vs. Oriented Bounding Boxes for 3D collisions. At first I didn't realize there were different kinds of bounding boxes and it wasn't until I dug into it and spent a couple days researching it on the Internet before I realized that I was trying to use my AABBs as if they were OBBs and that I needed to completely rethink my approach.

Sometimes what you need is someone just to give you the right term to lookup.

I haven't much gotten into AI; there have just been too many other things to learn in 3D programming that AI has gotten pushed to the back burner especially after I was somewhat forced to abandon XNA and relearn everything in DirectX. One of these days I'll get around to it; I hope.

I think there are some pretty good books on the subject out there but I haven't read any of them. I have this book. I haven't gotten around to reading it, like so many of the books on my shelves, but I've flipped through it and it looks pretty good. It's one of the reasons I'm vaguely aware of A* pathfinding.
Was This Post Helpful? 0
  • +
  • -

#6 BBeck   User is offline

  • Here to help.
  • member icon


Reputation: 792
  • View blog
  • Posts: 1,886
  • Joined: 24-April 12

Re: I have been trying to learn how to make AI in C++ so I made a game.

Posted 09 December 2014 - 08:15 PM

I didn't actually watch this. But I came across it and thought you might be interested in it.
Was This Post Helpful? 1
  • +
  • -

#7 taylorabc101   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-December 14

Re: I have been trying to learn how to make AI in C++ so I made a game.

Posted 10 December 2014 - 05:59 AM

Thanks I will watch it when I get home.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1