Sign in to follow this  

where shall i go from this

This topic is 4817 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

#include <iostream>
#include <ostream>
#include <istream>
#include <stdlib.H>

using std::cin;
using std::cout;

int main ()
{
int x=5;
int i;
int y=4;
int a;
cout << "enter the number five \n";
cin >> i;
if (i==x) {
cout << "well done! \n"; }
else {
cout << "i said type five dumbass \n"; }
cout << "now do the sum 5-1\n";
cin >> a;
if (a==y) {
cout << "well done! \n"; }
else {
cout << "The answer is four, shit-for-brains \n"; }
system("PAUSE");
return(0);
}

this is about the best of my abilities so far :) it's not much and i want to kno if you have to use 'using std::cin; using std::cout;' every time i make a source do i have to add this at the top? Also where do i go from this, i know i have to learn loops and functions so has anyone got any good tut sites? thnx!

Share this post


Link to post
Share on other sites
[source lang=cpp]#include <iostream>
#include <ostream>
#include <istream>
#include <stdlib.H>

using namespace std;

int main ()
{
int x=5;
int i;
int y=4;
int a;
cout << "enter the number five \n";
cin >> i;
if (i==x) {
cout << "well done! \n"; }
else {
cout << "i said type five dumbass \n"; }
cout << "now do the sum 5-1\n";
cin >> a;
if (a==y) {
cout << "well done! \n"; }
else {
cout << "The answer is four, shit-for-brains \n"; }
system("PAUSE");
return(0);
}



like this?

Share this post


Link to post
Share on other sites
Yes. As for where to go next. . . Try to increase the possible decisions. Change the theme a bit and you have tic-tac-toe. Or try to do a mastermind sim. Good luck.

Share this post


Link to post
Share on other sites
Well, if you learn loops, you shouldn't have any problems. You don't need an advanced graphics API to draw a tic tac toe board ;p

 X | O | X
-----------
O | X | X
-----------
O | O | X



[cool]
Good luck!

Share this post


Link to post
Share on other sites
You should learn to format your code, to make it more readable. The C++ compiler ignores whitespace (spaces, tabs and new lines), which means you have quite a lot of leeway in formatting. There isn't really any standard, but this is the way I would have formatted your code:
[source lang=cpp]#include <iostream>
#include <ostream>
#include <istream>
#include <stdlib.h>

using namespace std;

int main ()
{
int x = 5;
int y = 4;
int i;
int a;

cout << "enter the number five\n";
cin >> i;
if (i == x)
{
cout << "well done!\n";
}
else
{
cout << "i said type five dumbass\n";
}

cout << "now do the sum 5-1\n";
cin >> a;
if (a == y)
{
cout << "well done!\n";
}
else
{
cout << "The answer is four, shit-for-brains\n";
}

system("PAUSE");

return(0);
}



As for learning about loops and functions, I can only suggest seeing what Google dredges up. I bought a book to learn C++ (SAMS' Teach Yourself C++ In 21 Days) rather than using the internet, so I can't recommend any particular tutorials.

[Edited by - Gourgy on October 7, 2004 4:13:18 PM]

Share this post


Link to post
Share on other sites
You can make a simple version of tic tac toe that is completely text based. Basically, you need a way to output a grid like this:



X | X | X
O | X | X
O | O | O



The loops you would need for a game like this would be,

while
for



I'll explain how each one works, and how I would use it in a tic-tac-toe game. Hopefully it'll be easy to follow.

while loops:

Basically, a while loop says:

while something is true, execute some code. If it isn't true then don't execute the code.

The syntax for a while loop is as follows


while (expression)
{
execute code
}


A while loop is a very common way to control a game loop. In the case of tic tac toe, you would probably want a menu that allows you to either start a new game or quit the game. The menu might look something like this:


Haku's Tic-Tac-Toe

Menu
----

1) Start New Game
2) Quit

>


Here is an example of using a while loop to control this menu:


int menu_choice; // variable to hold the menu selection
int game_exit = 0; // this says we don't want to exit the game

while(game_exit != 1) // while game_exit <b>is not</b> == 1
{
// execute this code here
cout << "Haku's Tic-Tac-Toe\n" << endl;
cout << "Menu\n----\n" << endl;
cout << "1) Start New Game" << endl;
cout << "2) Quit\n" << endl;
cout << "> ";

cin >> menu_choice; // get the users selection

/* now we are going to check the users choice */
if (menu_choice == 1)
{
startNewGame(); // I haven't defined this yet, but I will
}
else if (menu_choice == 2)
{
game_exit = 1; // says we <b>do</b> want to exit the game
}
else // user entered something besides 1 or 2
cout << "Invalid Choice. Try Again.\n" << endl;
} // the end of the while loop






Now you have a simple while loop that loops forever until game_exit is set to 1. Now I'll talk about startNewGame().

Basically, this is the meat of the game. It will print out the current game board, ask the player where he want's to place the X, and check for 3 X's or O's in a row. It's going to look like this:


0 | 1 | 2
3 | 4 | 5
6 | 7 | 8

Position:


The player then types in the number 0-8 and an X is placed into that "square". This is how I would do it. Before doing any code, let me explain how a "for" loop works.

for loops are the hardest loops to grasp at first, because you can't say them in english the way they are written. By that I mean, in the case of an if statement, you can see that if(something) means "if something is true" or that while(something) means "while something is true."

An example of a for loop:


for(int i = 0; i < someNumber; ++i)
{
//execute code
}






This is how a for statement works:

int i = 0;

This is where you initialize any variables you need. Often used to iterate through an array, which I'll be doing below.

i < someNumber;

You can think of this section of a for loop as saying "if i is less than someNumber then execute code, otherwise exit out of the loop"

++i

Increases the value of i by 1. this is done each time the code in braces is executed. in this case, once i has been increased to the point where it is no longer less than someNumber, the for loop exits.

OK, I hope that made sense. Now here is how I would do the startNewGame() function.


// start by declaring variables

char pieceAt[9] = { '0','1','2','3','4','5','6','7','8'};
/* this will create an array of characters which we will use to keep track of which tiles have already been played, and whether an 'X' or 'O' occupies that space */

int gameOver = 0; // is the game over? Has someone won?

int position;

void startNewGame()
{
// nested loops can be complicated, try and follow what is
// going on here
while(gameOver != 1) // while gameOver isn't == 1
{

for(int i = 0; i < 9; i++)
{
if(i == 3 || i == 6 || i == 9)
cout << endl;
cout << pieceAt[i] << " | ";
}
cout << "\nPosition: "
cin >> position;

// you should now check to make sure it's a valid
// position, and that the position is available, i'll
// leave that to you ;)

/* at this point you would have the computer guess a
position*/


// and finally, check if either the computer or player
// has won, and if there are any spaces left.

// if someone has won, or if there are no spaces left,
// print the appropriate message to the screen (win, lose,
// tie, and then go back to the main menu by setting
// gameOver to 1.

// otherwise the loop starts at the top until one of those
// three situations occur

} // end of the while loop
} // end of the function






Hope you found this useful!

- heap

Share this post


Link to post
Share on other sites

This topic is 4817 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

Sign in to follow this