C# Tic-tac-toe ve minimax algoritması

fcokur

Centipat
Katılım
21 Temmuz 2022
Mesajlar
1
Daha fazla  
Cinsiyet
Erkek
Merhaba.
Veri yapıları ve algoritmalar çalışıyorum. Bilgisayarın minimax algoritmasıyla seçim yapmasını istiyorum aşağıdaki kodda.

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace xox.
{

 internal class Program.
 {
 static string[,] board = { { "1", "2", "3" }, { "4", "5", "6" }, { "7", "8", "9" } };
 const string userCharacter = "X";
 const string computerCharacter = "O";

 private static void Board()
 {
 Console.WriteLine(" | | ");
 Console.WriteLine(" {0} | {1} | {2}", board[0,0], board[0, 1], board[0,2]);
 Console.WriteLine("_____|_____|_____ ");
 Console.WriteLine(" | | ");
 Console.WriteLine(" {0} | {1} | {2}", board[1,0], board[1, 1], board[1, 2]);
 Console.WriteLine("_____|_____|_____ ");
 Console.WriteLine(" | | ");
 Console.WriteLine(" {0} | {1} | {2}", board[2, 0], board[2, 1], board[2, 2]);
 Console.WriteLine(" | | ");
 }

 static void Topbar()
 {
 Console.WriteLine("Welcome to XOX");
 Console.WriteLine();
 Console.Write("Player X: ");
 Console.WriteLine("Player O: ");
 }

 static void GamePlay()
 {
 List<int> choices = new List<int>();
 Console.WriteLine("Enter a number: ");
 int userchoice = Convert.ToInt32(Console.ReadLine());
 choices.Add(userchoice);
 int index = userchoice - 1;
 int row = 0;
 if (index > 0)
 {
 row = index / 3;
 }
 int column = userchoice % 3;
 if (column == 0)
 {
 column = 2;
 }
 else if (column == 2)
 {
 column = 1;
 }
 else.
 {
 column = 0;
 }
 board[row, column] = userCharacter;

 Console.Clear();

 Board();

 /*
 Minimax algorithm will be added.
 */
 }
 static void Main(string[] args)
 {
 Topbar();
 Board();
 GamePlay();
 Console.ReadLine();
 }

 }
}

Şöyle bir JS kodu buldum:

JavaScript:
function bestMove() {
 // AI to make its turn.
 let bestScore = -Infinity;
 let move;
 for (let i = 0; i < 3; i++) {
 for (let j = 0; j < 3; j++) {
 // Is the spot available?
 if (board[i][j] == "") {
 board[i][j] = ai;
 let score = minimax(board, 0, false);
 board[i][j] = "";
 if (score > bestScore) {
 bestScore = score;
 move = { i, j };
 }
 }
 }
 }
 board[move.i][move.j] = ai;
 currentPlayer = human;
}

let scores = {
 X: 10,
 O: -10,
 tie: 0,
};

function minimax(board, depth, isMaximizing) {
 let result = checkWinner();
 if (result !== null) {
 return scores[result];
 }

 if (isMaximizing) {
 let bestScore = -Infinity;
 for (let i = 0; i < 3; i++) {
 for (let j = 0; j < 3; j++) {
 // Is the spot available?
 if (board[i][j] == "") {
 board[i][j] = ai;
 let score = minimax(board, depth + 1, false);
 board[i][j] = "";
 bestScore = max(score, bestScore);
 }
 }
 }
 return bestScore;
 } else {
 let bestScore = Infinity;
 for (let i = 0; i < 3; i++) {
 for (let j = 0; j < 3; j++) {
 // Is the spot available?
 if (board[i][j] == "") {
 board[i][j] = human;
 let score = minimax(board, depth + 1, true);
 board[i][j] = "";
 bestScore = min(score, bestScore);
 }
 }
 }
 return bestScore;
 }
}

JS kodunu C#'a aktaramıyorum.
Yardımlarınız için şimdiden teşekkürler.
 

Technopat Haberler

Geri
Yukarı