Dasturiy injinering


Download 114.18 Kb.
bet2/2
Sana18.06.2023
Hajmi114.18 Kb.
#1560336
1   2
Bog'liq
Amaliy ishi 3-dif

Algoritm


Endi kodni yozish algoritmini muhokama qilamiz. Ushbu algoritm sizga istalgan dasturlash tilida kod yozishga yordam beradi . Keling, bu qanday amalga oshirilganini ko'rib chiqaylik.

  • 2 o'lchovli massivdan foydalanib doska yarating va har bir elementni bo'sh qilib ishga tushiring.

    • O'zingizga yoqqan har qanday belgi yordamida bo'sh joyni ko'rsatishingiz mumkin. Bu erda biz defisdan foydalanamiz. '-'.

  • Doska to'ldirilgan yoki yo'qligini tekshirish funksiyasini yozing.

    • Doskada takrorlang va falsetaxtada bo'sh belgi bo'lsa, qaytib keling yoki qaytaring true.

  • O'yinchi g'alaba qozongan yoki yo'qligini tekshirish uchun funktsiyani yozing.

    • Oldingi bo'limda muhokama qilgan barcha imkoniyatlarni tekshirishimiz kerak.

    • Barcha satrlar, ustunlar va ikkita diagonalni tekshiring.

  • Doskani ko'rsatish uchun funktsiyani yozing, chunki biz foydalanuvchilarga ular o'ynayotganda taxtani bir necha marta ko'rsatamiz.

  • O'yinni boshlash uchun funktsiyani yozing.

    • O'yinchining birinchi navbatini tasodifiy tanlang.

    • O'yin tugagandan so'ng uziladigan cheksiz tsiklni yozing (g'alaba yoki durang).

      • Keyingi harakat uchun joy tanlash uchun foydalanuvchiga doskani ko'rsating.

      • Foydalanuvchidan qator va ustun raqamini kiritishni so'rang.

      • Tegishli o'yinchi belgisi bilan joyni yangilang.

      • Joriy o'yinchi o'yinda g'alaba qozongan yoki yo'qligini tekshiring.

      • Agar joriy o'yinchi o'yinda g'alaba qozongan bo'lsa, unda g'olib xabarni chop eting va cheksiz tsiklni buzing.

      • Keyinchalik, taxta to'ldirilgan yoki yo'qligini tekshiring.

      • Agar taxta to'ldirilgan bo'lsa, unda chizish xabarini chop eting va cheksiz pastadirni buzing.

    • Nihoyat, foydalanuvchiga doskaning yakuniy ko'rinishini ko'rsating.

Siz nima bo'layotganini tasavvur qilishingiz mumkin. Xavotir olmang, hatto siz buni to'liq tushunmagan bo'lsangiz ham. Kodni ko'rganingizdan so'ng ko'proq ravshanlikka erishasiz.
Shunday qilib, kod bo'limiga o'tamiz. Kodni sinab ko'rish uchun kompyuteringizda Python o'rnatilgan deb o'ylayman .

2.
KODI: import random


class TicTacToe:


def __init__(self):
self.board = []


def create_board(self):
for i in range(3):
row = []
for j in range(3):
row.append('-')
self.board.append(row)


def get_random_first_player(self):
return random.randint(0, 1)


def fix_spot(self, row, col, player):
self.board[row][col] = player


def is_player_win(self, player):
win = None


n = len(self.board)


# checking rows
for i in range(n):
win = True
for j in range(n):
if self.board[i][j] != player:
win = False
break
if win:
return win


# checking columns
for i in range(n):
win = True
for j in range(n):
if self.board[j][i] != player:
win = False
break
if win:
return win


# checking diagonals
win = True
for i in range(n):
if self.board[i][i] != player:
win = False
break
if win:
return win


win = True
for i in range(n):
if self.board[i][n - 1 - i] != player:
win = False
break
if win:
return win
return False


for row in self.board:
for item in row:
if item == '-':
return False
return True


def is_board_filled(self):
for row in self.board:
for item in row:
if item == '-':
return False
return True


def swap_player_turn(self, player):
return 'X' if player == 'O' else 'O'


def show_board(self):
for row in self.board:
for item in row:
print(item, end=" ")
print()


def start(self):
self.create_board()


player = 'X' if self.get_random_first_player() == 1 else 'O'
while True:
print(f"Player {player} turn")


self.show_board()


# taking user input
row, col = list(
map(int, input("Enter row and column numbers to fix spot: ").split()))
print()


# fixing the spot
self.fix_spot(row - 1, col - 1, player)


# checking whether current player is won or not
if self.is_player_win(player):
print(f"Player {player} wins the game!")
break


# checking whether the game is draw or not
if self.is_board_filled():
print("Match Draw!")
break


# swapping the turn
player = self.swap_player_turn(player)


# showing the final view of board
print()
self.show_board()
# starting the game
tic_tac_toe = TicTacToe()
tic_tac_toe.start()
Kodning namunaviy chiqishini tekshiring.
$ python tic_tac_toe.py
Player X turn
- - -
- - -
- - -
Enter row and column numbers to fix spot: 1 1


Player O turn
X - -
- - -
- - -
Enter row and column numbers to fix spot: 2 1


Player X turn
X - -
O - -
- - -
Enter row and column numbers to fix spot: 1 2


Player O turn
X X -
O - -
- - -
Enter row and column numbers to fix spot: 1 3


Player X turn
X X O
O - -
- - -
Enter row and column numbers to fix spot: 2 2


Player O turn
X X O
O X -
- - -
Enter row and column numbers to fix spot: 3 3


Player X turn
X X O
O X -
- - O
Enter row and column numbers to fix spot: 3 2


Player X wins the game!


X X O
O X -
- X O
Kod tuzilishini tushunishga yordam beradigan ba'zi asosiy fikrlar.

  • Biz barcha usullarni bir joyda olish uchun sinfdan foydalandik. Bu boshqa kodlarda ham osongina qayta foydalanish mumkin bo'lgan to'plam bo'lishi mumkin.

  • Keyinchalik, kichik vazifa bo'lsa ham, har bir mas'uliyat uchun turli funktsiyalarni belgilab oldik. Bu kodni osongina saqlashga yordam beradi.

  • Yuqoridagi ikkita yondashuv, agar biz o'yinni yangilamoqchi bo'lsak, ilovani osongina yangilashimizga yordam beradi.

3. Xulosa: Tic Tac Toe o’yini mobil ilovasining dasturiy taminot moduli tuzildi.Bu jarayonda dastur algoritmi ham yozildi va kod terilib tekshirib kurildi.Bu o’yinni barchamiz sevib uynaganmiz.


Foydalingan adabiyotlar va tarmoqlar
1. https://geekflare.com/tic-tac-toe-python-code/
Download 114.18 Kb.

Do'stlaringiz bilan baham:
1   2




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling