Skip to content

realthdat/TicTacToe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 

Repository files navigation

Tic-Tac-Toe Game

Overview

This is a Windows Forms application written in C# that implements a Tic-Tac-Toe game (also known as Gomoku or Caro in some regions) on a 20x17 grid. The game supports two modes: Player vs. Player and Player vs. Computer. The objective is to align five symbols ("O" or "X") in a row, column, or diagonal to win. The game includes features like a timer for moves, an undo option, and a simple AI for the computer opponent.

Features

  • Game Modes:
    • Player vs. Player: Two players take turns placing "O" or "X" symbols.
    • Player vs. Computer: A single player competes against an AI opponent.
  • Game Board: A 20x17 grid where players place their symbols.
  • Timer: A cooldown timer enforces a time limit for each move, with a timeout resulting in the opponent winning.
  • Undo Move: Players can undo their last move (or last two moves in Player vs. Computer mode).
  • Win Detection: Automatically checks for five aligned symbols in rows, columns, or diagonals.
  • AI Opponent: Uses a heuristic scoring system to prioritize moves based on attack and defense strategies.
  • Visual Feedback: Hover effects and game-over background color changes enhance the user experience.

Prerequisites

  • .NET Framework: Ensure you have .NET Framework installed (compatible with the Windows Forms application).
  • Visual Studio: Recommended for building and running the project.
  • Resources: The project references image resources (o.png, x.png, o_img.png, x_img.png) in the Properties.Resources namespace. Ensure these are included in the project.

Setup Instructions

  1. Clone the Repository:
    git clone https://github.com/realthdat/TicTacToe.git
  2. Open the Project:
    • Open the solution file (TicTacToe.sln) in Visual Studio.
  3. Add Resources:
    • Ensure the required image files (o.png, x.png, o_img.png, x_img.png) are added to the project's resources.
  4. Build and Run:
    • Build the solution in Visual Studio (Ctrl+Shift+B).
    • Run the application (F5).
  5. Game Controls:
    • Use the menu strip to start a new game (Player vs. Player or Player vs. Computer).
    • Click on a grid cell to place a symbol.
    • Use the "Undo" option to revert the last move(s).
    • Select "Quit" to exit the game.

Project Structure

  • Form1.cs: The main form containing the game logic, UI setup, and event handlers.
    • Initializes a 20x17 grid of Label controls for the game board.
    • Manages game state (player, gameover, vsComputer, etc.).
    • Handles user interactions (clicks, hover effects, timer ticks).
    • Implements AI logic for the computer opponent using attack and defense scoring.
  • Chess.cs: A class representing a move on the board, storing the Label control and coordinates (X, Y).
  • Contain.cs: (Assumed) A static class defining constants like edgeChess (cell size).
  • Resources: Image files for "O" and "X" symbols used in the game.

How to Play

  1. Start a Game:
    • Choose "Player vs. Player" or "Player vs. Computer" from the menu.
  2. Make a Move:
    • Click an empty cell to place your symbol ("O" for Player 1, "X" for Player 2 or Computer).
    • A timer starts after each move. If it expires, the opponent wins.
  3. Undo a Move:
    • Click "Undo" to revert the last move (or last two moves in Player vs. Computer mode).
  4. Win Condition:
    • Align five symbols in a row, column, or diagonal to win.
    • The game ends with a message box announcing the winner, and the board turns gray.
  5. Quit:
    • Select "Quit" from the menu to exit the game.

AI Logic

The computer opponent uses a heuristic-based approach:

  • Scoring System:
    • Attack Scores: Prioritize moves that create or extend sequences of "X" symbols (e.g., 2, 3, or 4 in a line).
    • Defense Scores: Prioritize moves that block or counter sequences of "O" symbols.
    • Scores are defined in arrays Attack and Defense with increasing values for longer sequences.
  • Move Selection:
    • The AI evaluates all empty cells, calculating a combined attack and defense score.
    • It selects the cell with the highest score to place its "X" symbol.

Notes

  • The Contain class is referenced but not provided in the code. Ensure it defines edgeChess (e.g., public static int edgeChess = 30;) for the grid cell size.
  • The project assumes image resources are correctly set up in Properties.Resources.
  • The timer (tmCooldown) and progress bar (psbCooldownTime) must be configured in the form designer with appropriate properties (e.g., Maximum, Step).

Future Improvements

  • Add difficulty seviyeler for the AI (e.g., easy, medium, hard).
  • Enhance the UI with customizable board sizes or themes.
  • Implement a move history log or replay feature.
  • Add sound effects for moves and game-over events.

License

This project is for educational purposes. Feel free to modify and distribute as needed.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages