A simple tic tac toe program in C++ programming language.
I am currently accepting programming work, IT projects, school and application development, programming projects, thesis and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me at the following email address for further details. If you want to advertise on my website kindly contact me also in my email address also. Thank you.
My email address is the following jakerpomperada@gmail.com, jakerpomperada@aol.com, and jakerpomperada@yahoo.com.
My mobile number here in the Philippines is 09173084360.
Please subscribe to my channel https://www.youtube.com/channel/UCOs-lpOoIeJoh6gpJthPoGg
=================================================
You can buy my C++ book online at
https://www.mindshaperspublishing.com/product/beginners-guide-to-c-programming/
You can buy my book in introduction to computer networking at
https://www.unlimitedbooksph.com/product-page/introduction-to-computer-networking
Want to support my channel?
GCash Account
Jake Pomperada
09173084360
Paypal
https://paypal.me/jakerpomperada
Patreon
https://www.patreon.com/jakerpomperada
Thank you very much for your support.
#include <iostream> using namespace std; char board[3][3] = {{'1','2','3'}, {'4','5','6'}, {'7','8','9'}}; int player = 1; char mark; void draw_board() { cout << "Tic Tac Toe Game in C++" << endl; for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { cout << board[i][j] << " "; } cout << endl; } } void player_turn() { int choice; if(player % 2 == 1) { cout << "Player 1 turn (X): "; } else { cout << "Player 2 turn (O): "; } cin >> choice; switch(choice) { case 1: mark = 'X'; board[0][0] = mark; break; case 2: mark = 'X'; board[0][1] = mark; break; case 3: mark = 'X'; board[0][2] = mark; break; case 4: mark = 'X'; board[1][0] = mark; break; case 5: mark = 'X'; board[1][1] = mark; break; case 6: mark = 'X'; board[1][2] = mark; break; case 7: mark = 'X'; board[2][0] = mark; break; case 8: mark = 'X'; board[2][1] = mark; break; case 9: mark = 'X'; board[2][2] = mark; break; default: cout << "Invalid move" << endl; player_turn(); } } bool game_over() { // Check rows for(int i = 0; i < 3; i++) { if(board[i][0] == board[i][1] && board[i][1] == board[i][2]) { return true; } } // Check columns for(int j = 0; j < 3; j++) { if(board[0][j] == board[1][j] && board[1][j] == board[2][j]) { return true; } } // Check diagonals if(board[0][0] == board[1][1] && board[1][1] == board[2][2]) { return true; } if(board[0][2] == board[1][1] && board[1][1] == board[2][0]) { return true; } return false; } int main() { draw_board(); while(!game_over()) { player_turn(); draw_board(); player++; } if(player % 2 == 1) { cout << "Player 2 wins!" << endl; } else { cout << "Player 1 wins!" << endl; } return 0; }
No comments:
Post a Comment