Share your repls and programming experiences

← Back to all posts
My best/messiest code so far (work in progress)
etian (31)

So as no one knows, for school I've been working on a tic tac toe game and its pretty fun! I tried doing some coding now but I look through all the code and its completely fine but I guess my home wifi is too laggy. :/

Commentshotnewtop
sanjaykdragon (172)

Ok game I guess.
A few suggestions:
1. Use std::this_thread::sleep_for and std::chrono::duration (seconds, milliseconds, microseconds) to make your code more readable. It is cleaner than using usleep and milliseconds (and a seemingly random amount of MS at that)
2.Split up your file into classes / namespaces if needed.
3. Format the document with tabs and spaces. It looks bad to the eyes if it isn't formatted properly.
4. for "void begone()", you can use std::fill instead of a for loop

These are just comments on the code, as the code doesn't actually run.

VelascoCamila (0)

etian is in my class we did this in class with our teacher mr delvalle

SkyyCivil (92)

This is rlly cool!
you have a very creative mind!

Robotechnolgy (0)

c++ always makes things more difficult, I used js and I got a ai that I never was able to beat.

etian (31)

@Robotechnolgy
1.c++ is just different
2. the difficulty depends on what the progammer coded, i could do the same if i wanted to.
3. My teacher said don't make it unbeatable

TarunMalviya1 (0)

Ohh I won and it says i lost

etian (31)

Maybe I should just take down this post :/

Highwayman (204)

@etian why? This is a wonderful repository of knowledge and learning all supported upon the basis of your wonderful code, why would you destroy such a beautiful thing? If you take it down, no one will see your game or all these posts or anything else. No one here is admonishing you or trying to insult you in any way. In fact I guarantee that we all enjoy helping you. I personally apologize myself if I in any way insulted you. This is a good post, please don’t take it down.

etian (31)

@Highwayman it was smth like this for myother post

void checkwin()
{
for (int g=0;g<3;g++)
test=a[spot];
spot2++;
test2=a[spot];
spot2++;
test3=a[spot]
spot2++;
if (spot2==3)
{
spot2=0;
}
if (test == 'X' && test2 == 'X' && test3 == 'X')
{
cout << "Game over!\nYou won."
}

Highwayman (204)

@etian well it looks like you forgot to put a comma between spot2 and test3 on line 6, so there’s that...
Um, hmm..
you are trying to make test be equal to an array of characters, but it is an int. If you are thinking about the fact that test is at least 4 bytes long and a[n] will be only three bytes long, then I suggest putting in an actual cast to int so that that is more clear, if not, then that isn’t great, and I wouldn’t encourage it anyways, just use a new array of chars.

It seems like that’s the big thing there, you are make test 1 , 2 and 3 be equal to the arrays instead of the characters.

So

You created your array (a) like so.

// I want an array of arrays
char a[][] = { { 'T' ,  'i',   'c' },
                { 'T' , 'a' , 'c' },
                { 'T' , 'o' , 'e' } };

And then you set test to the first array of a, instead of the first element of the first array. Like so.

test = a[0]; // so you basically are setting it to { 'T' , 'i' , 'c' }; , instead of 'T' .
etian (31)

@Highwayman well i just fixed that up really quick itwasnt exactly like that i fixed all the simple errors and thx i forgot about that

nt998302 (5)

Good work Etian! Keep coding and you'll keep getting better. Also, just for quick reference, this code clears the screen:
cout << "\033[2J\033[1;1H";

etian (31)

@nt998302 Thank you! I was trying to find how to on the internet.

Highwayman (204)

@nt998302 , @etian guys please do not use that command, it is not portable and requires a video console, which not everyone is going to have. The actual name of that type of thing is escaping me at the moment, but just know that is part of a series of commands that are NOT portable and therefore unreliable. Also can be hurtful for methods like, say, output redirection, (piping, ya know all that good stuff. It will actually see that command - it won’t be invisible to it.)

mwilki7 (519)

Those are called ansi escape codes to modify the attributes of a console.
@etian If that command works for you feel free to use it. Repl isn't switching to Windows any time soon so I don't see how it will break the program.

sanjaykdragon (172)

@Highwayman you are correct, that sequence is not correct.

sanjaykdragon (172)

@mwilki7 a better solution is just to do system("clear");
although that is still not perfect and portable to other systems

Highwayman (204)

@sanjaykdragon nuuuuuu system("clear"); is even worse than ansi codes! That doesn’t even work on repl.it! Really a bunch of \ns is all they need. no need to actually manipulate the screen.

Highwayman (204)

@sanjaykdragon oh, oops, you already said that sorry about that.

Highwayman (204)

@Highwayman really if we are going to be doing any of this, why don’t we just use curses?

sanjaykdragon (172)

@Highwayman what? and spamming \n is way worse... cls/clear works on repl.it

Highwayman (204)

@sanjaykdragon ncurses.h? It makes stuff a bit more portable I think? pcurses for windows too, so it’s not just Unix systems I’m pretty sure.

Highwayman (204)

@sanjaykdragon not really. It does the same exact thing as clearing the screen, just instead of sending out a possibly unknown sequence and literally erasing stuff from the screen, it will just shove that stuff upwards. Besides, I personally just find it better when I have a history of sorts of my input instead of only the current stuff. No need to memorize sequences or include unnecessary headers (this is including cstdlib or ncurses.h, don’t get me wrong)

Highwayman (204)

@sanjaykdragon yeah probably just a typo on my part sorry bout that.

nt998302 (5)

@Highwayman I dislike using the ansi escape codes, but for some reason, the system("CLEAR"); command code doesn't work for me. :(

Highwayman (204)

@nt998302 did you just test it? the thing with system("anything") is that it has completely undefined behavior, so you never really know what you’re going to get and it can be a big security risk apparently, but don’t ask me about that because I’ve just heard that around, I don’t really know how it’s a big security risk.

sanjaykdragon (172)

@Highwayman using system("anything") is only a big security risk if the system command is based on user input

Highwayman (204)

@sanjaykdragon no. Regular commands by the programmer is also a security risk too.

etian (31)

@Highwayman this is all way too advanced for me

Highwayman (204)

@etian maybe. Not necessarily though. Don’t scare your self with strange words, most of it is quite simple.

an ansi escape code is basically anything that starts with '\e['. All it is is if you have a 'terminal emulator' or a 'video terminal', it interprets the stuff after '\e[' as a special command that is has to execute that does something. That thing can be changing the color of the text or changing the position of the cursor in the terminal or clearing the console... all sorts of things, and they all have to do with manipulation of the console screen.

system() is a function defined in the standard c library header (stdlib.h or cstdlib) and all it does is take your string that you pass it and send it as a command to the ... command processor? Idk, all I know is that it produces undefined behavior and that for repl.it it can take bash commands, so maybe command processor just means the shell?

When I say curses, I am referring to a group of header files (ncurses.h,curses.h,pcurses) that we’re created to provide a level of abstraction to manipulating the console and a certain level of portability.

system("CLEAR"); on some systems (hah) will clear the console.

Highwayman (204)

@Highwayman oops I just realized, I’m pretty 'pcurses' isn’t the right name, but whatever it’s close you get my point.

Highwayman (204)

Um.
usleep(777777);

Highwayman (204)

@etian sorry your right, should explain myself lol.

Is usleep doing seconds? Because if so, then why am I waiting for, like, hours? If not, then flush your streams after using any type of function that makes you wait - it just makes output show up. I don’t know why exactly for reasons I don’t care to explain, but it does work. So. Yeah.

Highwayman (204)

@Highwayman

cout << "Hello " << flush;
usleep(6738);
cout << "world." << flush;
nt998302 (5)

@Highwayman Usleep refers to Milliseconds

nt998302 (5)

@Highwayman Can You check out my game, it's called A Story. You seem like you know what you're doing and feedback would be great!

Highwayman (204)

@nt998302 sure, but I would take my suggestions with a grain of salt. I’m not terribly experienced.

etian (31)

@Highwayman o no haven't tried it yet I just didn't know how to respond.Thanks

Highwayman (204)

@etian XD oh lol. Yw! I hope if fixes it for you.

nt998302 (5)

Does anyone know how to code C++ in Unreal 4?

Highwayman (204)

@nt998302 thanks! I was wondering how I was going to tell you stuff.

etian (31)

@nt998302 I think microseconds

etian (31)

@Highwayman I figured out the problem, I had the > the wrong way :/

sanjaykdragon (172)

@etian use std::this_thread::sleep_for and std::chrono::seconds/milliseconds/microseconds instead of usleep and defaulting to milliseconds. Makes your code much more readable.