We’re rewarding the question askers & reputations are being recalculated! Read more.
58

Avoid platform specific code If there is an alternative you should try to avoid platform specific code. This entails avoiding #include <windows.h> and using things like system("pause"); which rely on commands the OS understands. There are guides on how to wait for user input on the command line and on how to clear the screen on multiple platforms on ...


31

This is not bad for a programmer as new to C++ as you have said you are. Keep up the good work! With that said, here are some ideas on how you might be able to improve your program. Don't abuse using namespace std Putting using namespace std at the top of every program is a bad habit that you'd do well to avoid. It's not necessarily wrong to use it, but ...


26

Here are some things that may help you improve your code. Fix your formatting The indentation, in particular, seems rather random. It may be that it's an artifact of posting the code, but it doesn't look very consistent. It's less important which coding convention you follow, than that you consistently follow some convention. Omit return 0 When a C or ...


26

Here are some observations and suggestions that may help you improve your program. Check your spelling If you run a spell check on your comments, you'll find a number of things such as "peice" instead of "piece" and "accpets" instead of "accepts". Since your code is nicely commented, it's worth the extra step to eliminate spelling errors. Separate ...


25

Here are some things that may help you improve your code. Use the required #includes The code uses std::string which means that it should #include <string>. It was not difficult to infer, but it helps reviewers if the code is complete. Have you run a spell check on comments? If you run a spell check on your comments, you'll find a number of things ...


24

A few comments: [ ... ] for (; (x % 3) != 0; x--); // quickly set x to left bound of sub-board for (; (y % 3) != 0; y--); // quickly set y to upper bound of sub-board I think I'd move the code to round to a multiple of three into a function of its own. I think I'd implement that something like this: int round3(int in) { return (in/3)*3; } [ ... ] int ...


24

Boolean values C99 has Boolean values: #include <stdbool.h>. Variable names int x=0; int j=0; x and j don't describe what they do. Is j the turn number? Also, I don't see x used anyway except this commented out line: //turn[j] = x;. Repetition I would set up a variable name to reduce all those ifs. Maybe something like: char token = (j % 2 == ...


22

Your code is quite good for someone who started 10 days ago. Here are a couple of improvements you should consider: Your code is hard to read due to inadequate indentation and spacing. Take a look here for the recommended indentation style for C++. using namespace std; is fine for simple programs like this, but it's a good idea to break out of the habit of ...


21

From a quick glance, this snippet looks like it could be extracted into its own parameterized function: printf("\n\n"); printf(" %c | %c | %c\n", board[0][0], board[0][1], board[0][2]); printf("-----------\n"); printf(" %c | %c | %c\n", board[1][0], board[1][1], board[1][2]); printf("-----------\n"); printf(" %c | %c | %c\n", board[2]...


18

First of all: nice work! It's easy to read and understand. Program organization It's very good that you split the task to small functions. Reading the body of main reveals nicely the overall flow. Ideas for further improvement: place does two things: it reads input from user and updates the state of the board. It would be good to separate these logically ...


17

Overall, I think it is a pretty good program. I think I see one bug. You don't check that the user's input for slotChoice is valid. If the input is outside the range 0..8, the program overruns the array. Some style and refactoring suggestions, in no particular order... I don't like the global GRID_SIZE constant. I would make a struct (or a class) that ...


17

Here are some observations that may help you improve your code. Use the appropriate #includes This code uses numeric_limits which is actually defined in <limits>. Even if your code compiles that way, it's probably only because one of the other files happened to include it. You can't and shouldn't rely on that, though. Use objects You have a ...


17

You have many problems within your code, including: Meaningless variable names (why, for example, is the user input named aaa?!); Repetition; Inconsistent whitespace; Repetition; Compound statements (the call the pa() should be on a separate line); and Repetition. I'd strongly recommend reading Python's style guide and following its guidance. However, your ...


16

Some of the comments are wrong, for example: #First, setup the board!! appears just before the draw_board function, which does not set up the board. The best practice here is to write a docstring for each function explaining what it does, how to call it, and what kind of values it returns. You use global variables board, user_win, computer_win, ties, ...


16

//Variable to store player, 0 is X, 1 is O. int counter = 0; Good thing that comment is there! This sets you up for some rather unimpressive player-turn logic though: //Check who's turn it is if(counter == 0) { button1.Text = "X"; counter++; }else if(counter == 1) { button1.Text = "O"; counter--; } So who the current player is is stored in ...


15

Yes, you really need to use loops and generalize this! In order for you to learn the most, I won't give any exact code but I will tell you a bit about what you need to do Your array seems to be index 1-9 based, I would recommend using index 0 to 8 instead. Array index always start at zero, it just seem like you're just not using it. Instead, use it. Use ...


15

What can I do to optimize reading the user input? Don't. For these kind of problems you will virtually never need to worry about time spent reading input. What can I do to optimize resource efficiency and how I am dealing with the data? I stored bytes instead of char which I think was pretty clever (I would had stored the board configuration in three ...


14

General: I myself am writing a Tic-Tac-Toe game of sorts, so I am go to try to not give my code away too much. For a self-taught beginning C programmer, this code looks good. However, it looks like you were over thinking things a bit based on how long your code is. What you did well on: You kept your dependencies at a minimum (for the most part). You ...


14

Here are some things that could help you improve your code. Don't abuse using namespace std Putting using namespace std at the top of every program is a bad habit that you'd do well to avoid. Use const variables instead of #define The use of #define to define constants in C++ is generally better avoided. Instead, for example, your code could have used ...


14

Notes: It seems that you don't need to store the input in the turn array You are not asserting legal user input (values between 1 and 9) You are not asserting that the user inputs a new value at every turn Since user input is a single character, you may as well scan for "%c" You can calculate the indexes to board based on the user input (turn) You can ...


14

Bug The same player can place its sign on a allready taken (by him) place. You have defended against this for the opponent although you use inappropriate language here. You should just check if the place in the grid is allready taken without checking who is currently playing. I would suggest using a method for this like private static bool ...


13

When confronted with a problem like this it helps to implement the chain-of-responsibility pattern. In this case, you have a hierarchy of sorts, with a winning move, defensive move, and a random move. You should create an interface along the lines of: public interface TicTacToeStrategy { public boolean apply(Button[] buttons); } Then, you should have ...


13

Some quick remarks: public properties should be PascalCase. use Auto-Implemented Properties. Capitalize the various parts of boxone etc., e.g. boxOne. Avoid using numbers in your names, e.g. box9. Do not start properties with an underscore: public bool _error. CheckWin() repeats the same code over and over again, surely it can be reduced to something far ...


13

What you've done here is listed all possible checks, but you've compiled it into a single method. Ideally, you should split this up into separate tasks, as there are three categories of similar checks: Horizontal checks Vertical checks Diagonal checks Maybe the current example is just simple enough to not fully warrant it, but especially if you had had ...


13

Avoid numbered variables char space_1 = ' '; char space_2 = ' '; char space_3 = ' '; char space_4 = ' '; char space_5 = ' '; char space_6 = ' '; char space_7 = ' '; char space_8 = ' '; char space_9 = ' '; int space_1y, space_1x; int space_2y, space_2x; int space_3y, space_3x; int space_4y, space_4x; int space_5y, space_5x; int space_6y, space_6x; int ...


12

There is not much to criticize in your code.... and there is a lot of code to go though. Mostly really neat, and, it takes a while to 'grok' it, but it comes together OK. Here's a laundry-list of small items (there are no big Items I can see): In TTUtils: getWinCondsWith is a method that is never called from anywhere getAllSmallestFields should be called ...


12

The code already looks nice. It is easy to understand and well structured. I would change these things: You define GRID_SIZE as 9. This made me immediately wonder: Why not 3?, since Tic Tac Toe is played on a 3 times 3 board. Usually the 'board size' is measured in one dimension only. Defining this constant is probably useless, unless you want to switch to ...


12

This doesn't make sense to me private bool isX; public bool isY { get { return isX; } set { isX = value; } } Are you checking for X or Y I assume you want 2 separate methods here or you just named these incorrectly. Here is where that incorrectly named variable comes ...


12

The code is full of repetition that can be removed. By just adding these two methods, it becomes much more readable. private boolean IsValue(int buttonNumber, String value) { return buttons[buttonNumber].getText().equals(value); } private void SetValue(int buttonNumber, String value) { buttons[buttonNumber].setText(value); buttons[buttonNumber]....


12

As JosephtheDreamer has left the Python to someone else, I'll take up the challenge. It's a small thing, but the styling for the definition of winning_combos doesn't seem right to me. We can see the start of the definition and what is inside, but the close parenthesis is hidden at the end of one of the lines. I prefer to put it on its own line: ...


Only top voted, non community-wiki answers of a minimum length are eligible