-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from Surajkumar5050/main
added Tic-Tac-Toe game using C++ programming language.
- Loading branch information
Showing
2 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
#include <iostream> | ||
using namespace std; | ||
|
||
// Function to display the Tic-Tac-Toe board | ||
void displayBoard(char board[3][3]) { | ||
for (int i = 0; i < 3; i++) { | ||
for (int j = 0; j < 3; j++) { | ||
cout << board[i][j]; | ||
if (j < 2) cout << " | "; | ||
} | ||
cout << endl; | ||
if (i < 2) cout << "---------" << endl; | ||
} | ||
} | ||
|
||
// Function to check if a player has won | ||
bool checkWin(char board[3][3], char player) { | ||
// Check rows and columns | ||
for (int i = 0; i < 3; i++) { | ||
if ((board[i][0] == player && board[i][1] == player && board[i][2] == player) || | ||
(board[0][i] == player && board[1][i] == player && board[2][i] == player)) { | ||
return true; | ||
} | ||
} | ||
|
||
// Check diagonals | ||
if ((board[0][0] == player && board[1][1] == player && board[2][2] == player) || | ||
(board[0][2] == player && board[1][1] == player && board[2][0] == player)) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
// Function to check if the board is full (a tie) | ||
bool isBoardFull(char board[3][3]) { | ||
for (int i = 0; i < 3; i++) { | ||
for (int j = 0; j < 3; j++) { | ||
if (board[i][j] == ' ') { | ||
return false; | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
int main() { | ||
char board[3][3] = { { ' ', ' ', ' ' }, { ' ', ' ', ' ' }, { ' ', ' ', ' ' } }; | ||
char currentPlayer = 'X'; | ||
int row, col; | ||
|
||
cout << "Welcome to Tic-Tac-Toe!" << endl; | ||
|
||
while (true) { | ||
// Display the current state of the board | ||
displayBoard(board); | ||
|
||
// Get the player's move | ||
cout << "Player " << currentPlayer << ", enter row (0, 1, or 2) and column (0, 1, or 2): "; | ||
cin >> row >> col; | ||
|
||
// Check if the input is valid | ||
if (row < 0 || row > 2 || col < 0 || col > 2 || board[row][col] != ' ') { | ||
cout << "Invalid move. Try again." << endl; | ||
continue; | ||
} | ||
|
||
// Make the move | ||
board[row][col] = currentPlayer; | ||
|
||
// Check for a win or a tie | ||
if (checkWin(board, currentPlayer)) { | ||
displayBoard(board); | ||
cout << "Player " << currentPlayer << " wins! Congratulations!" << endl; | ||
break; | ||
} else if (isBoardFull(board)) { | ||
displayBoard(board); | ||
cout << "It's a tie! The game is over." << endl; | ||
break; | ||
} | ||
|
||
// Switch to the other player | ||
currentPlayer = (currentPlayer == 'X') ? 'O' : 'X'; | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import java.util.Scanner; | ||
|
||
class ListNode { | ||
int val; | ||
ListNode next; | ||
|
||
ListNode(int val) { | ||
this.val = val; | ||
this.next = null; | ||
} | ||
} | ||
|
||
public class MergeSortedLinkedLists { | ||
public static ListNode merge(ListNode l1, ListNode l2) { | ||
ListNode dummy = new ListNode(0); | ||
ListNode current = dummy; | ||
|
||
while (l1 != null && l2 != null) { | ||
if (l1.val < l2.val) { | ||
current.next = l1; | ||
l1 = l1.next; | ||
} else { | ||
current.next = l2; | ||
l2 = l2.next; | ||
} | ||
current = current.next; | ||
} | ||
|
||
if (l1 != null) { | ||
current.next = l1; | ||
} else { | ||
current.next = l2; | ||
} | ||
|
||
return dummy.next; | ||
} | ||
|
||
public static ListNode createList() { | ||
Scanner scanner = new Scanner(System.in); | ||
System.out.print("Enter the number of elements: "); | ||
int n = scanner.nextInt(); | ||
|
||
ListNode dummy = new ListNode(0); | ||
ListNode current = dummy; | ||
|
||
System.out.println("Enter the sorted list elements:"); | ||
for (int i = 0; i < n; i++) { | ||
int val = scanner.nextInt(); | ||
current.next = new ListNode(val); | ||
current = current.next; | ||
} | ||
|
||
return dummy.next; | ||
} | ||
|
||
public static void printList(ListNode head) { | ||
ListNode current = head; | ||
while (current != null) { | ||
System.out.print(current.val + " -> "); | ||
current = current.next; | ||
} | ||
System.out.println("null"); | ||
} | ||
|
||
public static void main(String[] args) { | ||
System.out.println("Enter the first sorted list:"); | ||
ListNode l1 = createList(); | ||
System.out.println("Enter the second sorted list:"); | ||
ListNode l2 = createList(); | ||
|
||
System.out.println("Merged List:"); | ||
ListNode mergedList = merge(l1, l2); | ||
printList(mergedList); | ||
} | ||
} |