본문 바로가기

카테고리 없음

Dev C++ Tic Tac Toe Program



So I've been building a Tic-Tac-Toe program, it's still a work in progress at the moment. I'm not getting any build errors so far, but the main problem I've got at the moment is that the program just closes when player 1 enters their first number. Tic Tac Toe Game Project in C Explanation: This C program on TIC TAC TOE GAME is a simple text base game. This program is without graphics to focus on logic /algorithm used in game. Two players can play this game. Working code is attached and tested in visual studio 2015 Code.

Hey guys, I just learned the very basics of arrays and I have to write this program of a tic-tac-toe game. The requirements are this:

C++ tic tac toe program

Write a modular program that allows two players to play a game of tic-tac-toe. Use a two-dimensional char array with 3 rows and 3 columns as the game board. Each element of the array should be initialized with an asterisk(*). The program should display the initial board configuration and then start a loop that does the following:

Allow player 1 to select a location on the board for an X by entering a row and column number. Then redisplay the board with an X replacing the * in the chosen location

If there is no winner yet and the board is not yet full, allow player 2 to select a location on the board for an O by entering a row and column number. Then redisplay the board with an O replacing the * in the chosen location.


The Loop should continue until a player has won or a tie has occurred, then display a message indicating who won, or reporting that a tie has occurred.

Player 1 wins when there are three Xs in a row, a column, or diagonal on the game board
Player 2 wins when there are three Os in a row, a column, or diagonal on the game board
A tie occurs when all locations on the board are full, but there is no winner.

Input Validation: Only allow legal moves to be entered. The row must be 1, 2, or 3. The column must be 1, 2, or 3. The(row,column) position entered must currently be empty(i.e., still have an asterisk in it).

Okay, that is all. My problem is that I barely know how to use arrays. So what I was thinking is that maybe I should make an array as a function and in that function, make a loop for when I call it in my main function. That is where I am starting to get at. Are there any suggestions as to how else I can start this program? I just need guidance, not the code itself. I want to make the code myself, but I feel like I will need help. Any suggestion will be appreciated. Thank you.

  • 5 Contributors
  • forum 8 Replies
  • 1,956 Views
  • 3 Years Discussion Span
  • commentLatest Postby マーズ maazuLatest Post

Dev C++ Tic Tac Toe Program

mazzica1-1

i think it is easy:
1- you declare 2d array
2- fill it with *
3- declare player1Turn =true
4- declare player2Turn =false
5- make while loop where not finished
BS:not Finished would be function return bool that checks the condtions
6-if player1Turn
6.1- player1Turn =false
6.2- player2Turn =true
6.3- call function called play with 'player1','X' as args
6-else if player2Turn
6.1- player2Turn =false
6.2- player1Turn =true
6.3- call function called play with 'player2','O' as args
5- end while loop

#TIC TAC TOE

A simple TicTacToe program with nice console output written entirely in C++.

Tac

##IntroductionA Tic Tac Toe program lets the user choose his/her piece on a 3X3 board. Your goal is to align 3 of the same piece in a row/column/diagonal to win the game.

The computer (BAD GUY) will try to either win its own by doing the same, or it will block your winning ways.

Try to beat it!

##Cool PointsIf you tried to play this game, you will notice some very interesting colors on the console, as well as the game's almost god-like AI.

How did I do it, you ask?

Dev C++ Code For Tic Tac Toe

  • Console colors for the beautiful gameplay
  • Minimax algorithm, Read Wikipedia entry here for the God-like AI

Tic Tac Toe Dev C++ Code

##ClassesThere are three distinct classes in this TicTacToe game:

  • Game - This is the main game logic with minimax algorithm implemented
  • Board - This is the board for the game, has 3X3 dimensions
  • chesspiece - This is the piece that will be used for the game. Could have read 'TicTac' and 'Toe' but I decided that chesspiece is a little more generic.

Tic Tac Toe In C

Finally, there is one important class that runs the game: tictactoegame.

Tic Tac Toe C Programming

##How to runThis is written in C++, so to compile it, you need g++. The instructions below are for Mac/Linux:

  • log into Terminal
  • cd to the folder containing this game
  • type: make for easy compilation (I already included all the necessary files to make it easy for you to compile)[Alternatively: you can type g++ tictactoeGame.cpp Board.cpp Game.cpp chesspiece.cpp -o game to compile the game]
  • type: ./game to compile the game and you're done!

For Windows users, I think you might need Cygwin to achieve something similar. I haven't done a lot of programming in Windows, so that's a foreign land for me. For anyone intending to run this in Windows, please let me know and I can find out more about that.

C++ Tic Tac Toe Program

[Last updated: July 13, 2013]