-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtic.swift
126 lines (110 loc) · 3.54 KB
/
tic.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//
// tic.swift
// GeoPac
//
// Created by Scholar on 6/13/24.
//
import SwiftUI
import Foundation
//struct tic: View {
//import Foundation
// Enum to represent the players
enum Player {
case X
case O
}
// Enum to represent the state of each cell in the board
enum CellState {
case empty
case filled(Player)
}
// Function to print the current state of the board
func printBoard(board: [[CellState]]) {
for row in board {
for cell in row {
switch cell {
case .empty:
print("_", terminator: "\t")
case .filled(let player):
switch player {
case .X:
print("X", terminator: "\t")
case .O:
print("O", terminator: "\t")
}
}
}
print()
}
}
// Function to check if a player has won
func checkWin(board: [[CellState]], player: Player) -> Bool {
// Check rows
for row in board {
if row.filter({ if case .filled(player) = $0 { return true } else { return false }}).count == 3 {
return true
}
}
// Check columns
for columnIndex in 0..<3 {
if board[0][columnIndex] == .filled(player) &&
board[1][columnIndex] == .filled(player) &&
board[2][columnIndex] == .filled(player) {
return true
}
}
// Check diagonals
if board[0][0] == .filled(player) && board[1][1] == .filled(player) && board[2][2] == .filled(player) {
return true
}
if board[0][2] == .filled(player) && board[1][1] == .filled(player) && board[2][0] == .filled(player) {
return true
}
return false
}
// Function to check if the board is full
func isBoardFull(board: [[CellState]]) -> Bool {
for row in board {
for cell in row {
if case .empty = cell {
return false
}
}
}
return true
}
// Function to play the game
func playGame() {
var currentPlayer: Player = .X
var board: [[CellState]] = Array(repeating: Array(repeating: .empty, count: 3), count: 3)
print("Let's play Tic Tac Toe!")
while true {
printBoard(board: board)
print("Player \(currentPlayer)'s turn. Enter row and column (e.g., 1 2): ")
if let input = readLine(),
let position = input.split(separator: " ").map({ Int($0)! - 1 }),
position.count == 2,
position.allSatisfy({ $0 >= 0 && $0 < 3 }),
board[position[0]][position[1]] == .empty {
board[position[0]][position[1]] = .filled(currentPlayer)
if checkWin(board: board, player: currentPlayer) {
printBoard(board: board)
print("Player \(currentPlayer) wins!")
break
} else if isBoardFull(board: board) {
printBoard(board: board)
print("It's a draw!")
break
}
currentPlayer = (currentPlayer == .X) ? .O : .X
} else {
print("Invalid input. Try again.")
}
}
}
// Start the game
playGame()
}
#Preview {
tic()
}