PDA

View Full Version : i made this c++ in visual studio... how can i get it to work in xcode




DaKyd55
Apr 21, 2011, 06:25 PM
i made this last summer in windows visual studio express... can you alter the code so that it works in xcode?

here is the code:



#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <ctime>
#include <conio.h>
#include <string>

/*
skeleton for ascii games: displays an array of ascii art, refreshing every N milliseconds(n=300 in the example);
also checks for keypresses every refresh.
*/
using namespace std;

//display array:
const int x=40, y=40;
char display[x][y];
int personx=20;
int persony=20;
string blocks[y]={
" V V V ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" "
};

int score=0;
void redraw();//redraw screen

int main()
{

system("color 06");
srand((unsigned int)time(0));// random number seed

while (true)
{
int timestep=30;//timestep in miliseconds

//initialize screen:
for(int i=0; i<x; i++)
for(int j=0; j<y; j++)
display[i][j]=' ';

for(int i=0; i<x; i++)
for(int j=0; j<y; j++)
blocks[j][i]=' ';



//console size setup:
COORD bufferSize = {110+1, x+1};
SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), bufferSize);
SMALL_RECT WinRect = {0, 0, 100, x};
SetConsoleWindowInfo(GetStdHandle(STD_OUTPUT_HANDLE), true, &WinRect);

bool displayText = true;
while(!_kbhit()){
clock_t waketime=clock()+300;
while(clock()<waketime);//wait until taking the next step
system("cls");
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " ::: ::::::::::: ::::::::: :::::::: ::::::::::: ::::::::: ::::::::::: ::: ::: :::::::::: " << endl;
cout << " :+: :+: :+: :+: :+: :+: :+: :+: :+: :+: :+: :+: :+: :+: " << endl;
cout << " +:+ +:+ +:+ +:+ +:+ +:+ +:+ +:+ +:+ +:+ +:+ +:+ +:+ " << endl;
cout << "+#++:++#++: +#+ +#++:++#: +#++:++#++ +#+ +#++:++#: +#+ +#++:++ +#++:++# " << endl;
cout << "+#+ +#+ +#+ +#+ +#+ +#+ +#+ +#+ +#+ +#+ +#+ +#+ +#+ " << endl;
cout << "#+# #+# #+# #+# #+# #+# #+# #+# #+# #+# #+# #+# #+# #+# " << endl;
cout << "### ### ########### ### ### ######## ### ### ### ########### ### ### ########## " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " 1st player 2nd player " << endl;
cout << " A --> left J --> left don't get hit by the missiles --> V " << endl;
cout << " D --> right L --> left made by Sean Panayi " << endl;
if (displayText==true)
{
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " Press 1 For Single Player or 2 for Multiplayer " << endl;
}
displayText = !displayText;
}
bool twoplayer=true;
if(_getch()=='1')
{
twoplayer = false;
}
else
{
twoplayer = true;
}
system ("cls");
cout << " " << endl;
cout << " " << endl;
cout << " __ __ _ _ " << endl;
cout << " / |_ _ _ _ _ \\_/_ _ | \\.(_(_. _ ||_ " << endl;
cout << " \\__| )(_)(_)_)(- |(_)|_|| |__/|| | |(_|_|||_\\/ " << endl;
cout << " / " << endl;
cout << " " << endl;
cout << " beginner --> 1 intermediate --> 2 advanced --> 3 " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;

while(!kbhit){};
char level = _getch();
if (level=='1') timestep=100;
if (level=='2') timestep=35;
if (level=='3') timestep=10;


//some code that lets the user pick one or two player goes here.

if(twoplayer)
{
personx=1;
persony=39;
}
else
{
personx=20;
}

//console size setup:
COORD bufferSize2 = {x+1, y+1};
SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), bufferSize2);
SMALL_RECT WinRect2 = {0, 0, x, y};
SetConsoleWindowInfo(GetStdHandle(STD_OUTPUT_HANDLE), true, &WinRect2);

//main loop:
while(true)
{
score=score+1;
if (level=='1') score=score+1;
if (level=='2') score=score+2;
if (level=='3') score=score+3;
clock_t waketime=clock()+timestep;
while(clock()<waketime);//wait until taking the next step

//process keypress, if any:
if(_kbhit())//if there was a key press
{
char key=(char)_getch();//get which key it was
switch(key)
{
case 'a':
personx=personx-1;
break;
case 'd':
personx=personx+1;
break;
case 'j':
persony=persony-1;
break;
case 'l':
persony=persony+1;

}
}
//process game logic here


int g=rand() % x;
blocks[0][g]='V';
for(int i=0; i<x; i++)
for(int j=0; j<y; j++)
display[i][j]=blocks[j][i];

if(display [personx][39]==' ' || display [personx][39]=='Y' )
{
display [personx][39]='T';
}
else
{
break;
}
if(twoplayer)
{
if(display [persony][39]==' ' || display [persony][39]=='T' )
{
display [persony][39]='Y';
}
else
{
break;
}
}


redraw();

for (int i=y-1; i>0; i--)
{
blocks [i]=blocks [i-1];
}
blocks [0]=" ";

}
system("cls");
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " _______ ___ .___ ___. _______ " << endl;
cout << " / _____| / \\ | \\/ | | ____| " << endl;
cout << "| | __ / ^ \\ | \\ / | | |__ " << endl;
cout << "| | |_ | / /_\\ \\ | |\\/| | | __| " << endl;
cout << "| |__| | / _____ \\ | | | | | |____ " << endl;
cout << " \\______| /__/ \\__\\ |__| |__| |_______| " << endl;
cout << " " << endl;
cout << " ______ ____ ____ _______ .______ " << endl;
cout << " / __ \\ \\ \\ / / | ____|| _ \\ " << endl;
cout << "| | | | \\ \\/ / | |__ | |_) | " << endl;
cout << "| | | | \\ / | __| | / " << endl;
cout << "| `--' | \\ / | |____ | |\\ \\----. " << endl;
cout << " \\______/ \\__/ |_______|| _| `._____| " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " your score is "<< score << endl;
cout << " " << endl;
cout << " " << endl;
cout << " press spacebar to continue " << endl;
cout << " " << endl;
score=0;
while(!_kbhit() || _getch()!=' ' ){
}
}

}

void redraw()
{
system("cls");
for(int i=0; i<y; i++)
{
for(int j=0; j<x; j++)
{
cout << display[j][i];
}
cout << endl;
}
cout << score;



Mac_Max
Apr 21, 2011, 07:13 PM
You need to replace all the calls to functions in Windows.h and remove/replace anything using the System() function.

DaKyd55
Apr 21, 2011, 08:27 PM
You need to replace all the calls to functions in Windows.h and remove/replace anything using the System() function.

so how exactly do i do that? are there certain mac functions to replace the windows ones/ where can i get them? also what do you mean about the System() function

sorry i dont know a whole ton about this stuff

chown33
Apr 21, 2011, 09:18 PM
also what do you mean about the System() function

Actually, it's the system() function, because C (and C++, and Objective-C) is case-sensitive.

The system() function runs another command, as if typed into a command-line. None of the commands in your Windows code exist on Mac OS X. So you can't run them. For example, find the word system in the posted code, and make a list of the commands being executed. They're things like "cls" and "color 06". Those don't exist except on Windows.


sorry i dont know a whole ton about this stuff
Then you should probably spend some time studying it, so you know what it means.

kainjow
Apr 21, 2011, 09:37 PM
You can use the ncurses/curses library to do this.

Here's one way to do the main screen:

// gcc -lcurses airstrike.c -o airstrike

#include <ncurses.h>

int main()
{
WINDOW *w = initscr();
noecho();
nodelay(w, true);

int displayText = 1;
for (;;) {
clear();
printw("\n");
printw("\n");
printw("\n");
printw(" ::: ::::::::::: ::::::::: :::::::: ::::::::::: ::::::::: ::::::::::: ::: ::: ::::::::::\n");
printw(" :+: :+: :+: :+: :+: :+: :+: :+: :+: :+: :+: :+: :+: :+: \n");
printw(" +:+ +:+ +:+ +:+ +:+ +:+ +:+ +:+ +:+ +:+ +:+ +:+ +:+ \n");
printw("+#++:++#++: +#+ +#++:++#: +#++:++#++ +#+ +#++:++#: +#+ +#++:++ +#++:++# \n");
printw("+#+ +#+ +#+ +#+ +#+ +#+ +#+ +#+ +#+ +#+ +#+ +#+ +#+ \n");
printw("#+# #+# #+# #+# #+# #+# #+# #+# #+# #+# #+# #+# #+# #+# \n");
printw("### ### ########### ### ### ######## ### ### ### ########### ### ### ##########\n");
printw("\n");
printw("\n");
printw(" 1st player 2nd player \n");
printw(" A --> left J --> left don't get hit by the missiles --> V \n");
printw(" D --> right L --> left made by Sean Panayi \n");
printw("\n");
printw("\n");
printw("\n");
printw("\n");
if (displayText) {
printw(" Press 1 For Single Player or 2 for Multiplayer \n");
} else {
printw("\n");
}
displayText = !displayText;

int key = getch();
if (key == '1' || key == '2') {
break;
}

napms(500);
}

endwin();

return 0;
}

DaKyd55
Apr 21, 2011, 10:22 PM
You can use the ncurses/curses library to do this.

Here's one way to do the main screen:

(the code you entered)


so to be clear you're replacing my functions with mac ones? with the opening page that you posted i tried it and got 9 errors

can i run visual studio express with wine or something

kainjow
Apr 22, 2011, 01:57 AM
so to be clear you're replacing my functions with mac ones?
No, they aren't Mac-specific functions, they're part of the curses (http://en.wikipedia.org/wiki/Curses_(programming_library)) library which is included in Mac OS X and I'm guessing most Linux systems as well. If you google around it seems to be available for Windows, but that's not necessary as you already have it working.

with the opening page that you posted i tried it and got 9 errors
You tried it on Mac OS X? How? Please be specific.

can i run visual studio express with wine or something
Why would you want to do this?

DaKyd55
Apr 22, 2011, 07:31 AM
No, they aren't Mac-specific functions, they're part of the curses (http://en.wikipedia.org/wiki/Curses_(programming_library)) library which is included in Mac OS X and I'm guessing most Linux systems as well. If you google around it seems to be available for Windows, but that's not necessary as you already have it working.


You tried it on Mac OS X? How? Please be specific.

i just pasted it into xcode and tried to build and run it

Why would you want to do this?

in case it would let me run this stuff without having to change it