![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Dec 2006
Posts: 5
Rep Power: 0
![]() |
Hey everyone, I知 new to this forum.
I知 sure there are countless examples of how to do a tic-tac-toe game but I知 not worried about copying and pasting code. I致e done my own little rendition of it but I知 stuck at the following spot. I知 stuck trying to figure out how the computer will react to any move other than the first and second. Here is my code so far hopefully you guy can give me an example of what to do from here and maybe if you see something that is inefficient let me know and ill fix it. cpp Syntax (Toggle Plain Text)
|
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3
![]() |
The is the C++ forum, so while you can use the stdio.h function you can also use IO streams. Like "int val; cin >> val" is the same as "scanf("%d",&val"). That's not really important the more important thing is that where you get input from the user you don't check to make sure it was valid. This involves checking the return value of scanf, it is actually important. If scanf were to fail, you program would act randomly because you also don't provide default values for any variable user input is supposed to fill.
Last edited by Game_Ender; Dec 19th, 2006 at 7:57 PM. |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Dec 2006
Posts: 5
Rep Power: 0
![]() |
I’m running Visual Studio C++ 2005 Express and when I clicked file->new project console application that include is what I got, I could try it with iostream instead but I’m unfamiliar with the way “<<” works printf seems more common to the way I code. But other than that the error checking would seem like a big problem. Error checking was never one of my main goals but that’s only because my teachers were lazy and didn’t assume anyone would put anything in besides the required values. I’ll go back through my code and add error checking. Also I was thinking about strategies for tic-tac-toe and will probably change the void computer move to treat any move value equal to or less than one as moving in corner positions that way its harder for the user if they go second.
|
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
You can find at least 3 implementations of Tic Tac Toe by searching the forum. They aren't all in the same language, but you should be able to pick up generalized approaches.
Like GameEnder, I would suggest that if you are going to use C++, you use C++ library functions. You will often find them to be more effective or powerful than the C equivalents (or near equivalents). Always read the documentation for the functions you use THOUROUGHLY. The high-dollar geeks who wrote the functions put those return values and error-checking functions there for a reason. If you skip error checking at this point, the price you pay will probably only be having your app run off into the weeds and puke on its shoes, or melt into a slag heap while eating your dog and homework. Please advance past the schlock, however, before you take on any medical equipment or space-shuttle stuff.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Dec 2006
Posts: 5
Rep Power: 0
![]() |
Okay, I was unaware of which includes where c++ or c and I should make it practice to not let the overhead application add code that I have no idea what it does or where it came from. I suppose now would be a good time to add that I never really was taught in c++ not because I never chose to take a class on it just because my college never offered a class in it but that’s beside the point. Anyways, I’ve learned Java and Visual Basics so I know some fundamentals of Object Oriented Programming.
The problem I’m having is as follows. When I changed my include to: #include <iostream> it spit out an error saying that it was looking for stdafx.h? The exact error is as follows: fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source? |
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
I suggest you turn off precompiled headers (look through the compiler options) and dump stdafx. It is nothing, at this point, as you'll see if you read it. With precompiled header off, it won't be automatically generated and placed in the project, next time. I usually find it's best to open the project as an empty project, also. I'm not a huge fan of auto-generated stuff, though it can sometimes ease the process for a novice.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#7 |
|
Newbie
Join Date: Dec 2006
Posts: 5
Rep Power: 0
![]() |
Okay done, I found precompiled headers in the ".vcproj" file when I opened it with notepad it was set to two and I simply changed that to zero and bam works. Duly noted I will now build projects as empty and add my own source and header files. My include file now works although it is having a problem with cout and cin? Not sure why errors are as follows:
error C2065: 'cout' : undeclared identifier error C2065: 'cin' : undeclared identifier This is what those errors are probably pointing to: cout >> "\nHow many places over (0-2): "; cin >> posX; Last edited by Disruptor; Dec 19th, 2006 at 8:44 PM. |
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
In C++ the things that are included in, for instance, iostream, belong to a namespace called std:. This allows you to distinguish between the standard library's cin and a function or name that you might invent called "cin". You resolve this by specifying which "cin" you mean. You may qualify it by means of a scope resolution operator, such as "std::cin". This would be a different thing than "myNamespace::cin". The quickest way to resolve this is to put, at the top of your file, "using namespace std;". This is not a good solution, since it tosses all symbols in the std namespace into your area. If you happen to use a symbol in the std namespace by accident (and there are tons of them), you are in deep doo doo. You can pick and choose by using such things as "using std::cin". Then you can reference cin without the scope resolution. Just don't make your own label or function named "cin" unless you remember to qualify IT, by saying something like "myNamespace::cin".
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#9 |
|
Newbie
Join Date: Dec 2006
Posts: 5
Rep Power: 0
![]() |
Okay so changing my code to reference every single cin and cout? Instead of “using namespace std;” Would that be more efficient or just more explicit in declaring what I want? Also would I type “using std::cin” or just “std::cin” because I figured out that std:: gives me a huge list of commands from the namespace std.
|
|
|
|
|
|
#10 |
|
Newbie
Join Date: Feb 2006
Posts: 20
Rep Power: 0
![]() |
The difference between the 3 styles is the difference between whether or not you fill up your program's namespace scope with identifiers from the standard library.
The main idea of namespaces is to eliminate name clashes - so good practice is to make sure that identifiers remain confined to their own namespace, and aren't potentially clashing with other parts of the program for this reason, its best to explicitly qualify every identifier from the standard library by prefixing it std:: - and avoid the using keyword entirely. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|