- 31 Mar 2023
- 79
- 38
Python ile basit XOX oyunu.
Python:
from functions import *
def main():
print('XOX Oyununa hoş geldiniz.')
while True:
tahta = [' '] * 10
oyuncu1_isaret, oyuncu2_isaret = oyuncu_girdisi()
sirayi_sec = ilk_sirayi_sec()
print(sirayi_sec + ' önce gidecek.')
oyun_hazir = input('Oynamaya hazır mısınız? E veya H girin.')
if oyun_hazir.lower()[0] == 'e':
oyun_devam = True
else:
oyun_devam = False
while oyun_devam:
if sirayi_sec == 'Oyuncu 1':
tahtayi_goster(tahta)
pozisyon = oyuncu_secimi(tahta)
isaretle(tahta, oyuncu1_isaret, pozisyon)
if kazanan_mi(tahta, oyuncu1_isaret):
tahtayi_goster(tahta)
print('Tebrikler! Oyunu kazandınız!')
oyun_devam = False
else:
if tahta_dolu(tahta):
tahtayi_goster(tahta)
print('Oyun berabere bitti!')
break
else:
sirayi_sec = 'Oyuncu 2'
else:
tahtayi_goster(tahta)
pozisyon = oyuncu_secimi(tahta)
isaretle(tahta, oyuncu2_isaret, pozisyon)
if kazanan_mi(tahta, oyuncu2_isaret):
tahtayi_goster(tahta)
print('Oyuncu 2 kazandı!')
oyun_devam = False
else:
if tahta_dolu(tahta):
tahtayi_goster(tahta)
print('Oyun berabere bitti!')
break
else:
sirayi_sec = 'Oyuncu 1'
if not tekrar_oyna():
break
if __name__ == '__main__':
main()
functions.py
Python:
import random
def tahtayi_goster(tahta):
print(tahta[7] + '|' + tahta[8] + '|' + tahta[9])
print('-|-|-')
print(tahta[4] + '|' + tahta[5] + '|' + tahta[6])
print('-|-|-')
print(tahta[1] + '|' + tahta[2] + '|' + tahta[3])
def oyuncu_girdisi():
isaret = ''
while not (isaret == 'X' or isaret == 'O'):
isaret = input('Oyuncu 1: X mi yoksa O mu olmak istiyorsun?').upper()
if isaret == 'X':
return ('X', 'O')
else:
return ('O', 'X')
def isaretle(tahta, isaret, position):
tahta[position] = isaret
def kazanan_mi(tahta, isaret):
return ((tahta[7] == isaret and tahta[8] == isaret and tahta[9] == isaret) or
(tahta[4] == isaret and tahta[5] == isaret and tahta[6] == isaret) or
(tahta[1] == isaret and tahta[2] == isaret and tahta[3] == isaret) or
(tahta[7] == isaret and tahta[4] == isaret and tahta[1] == isaret) or
(tahta[8] == isaret and tahta[5] == isaret and tahta[2] == isaret) or
(tahta[9] == isaret and tahta[6] == isaret and tahta[3] == isaret) or
(tahta[7] == isaret and tahta[5] == isaret and tahta[3] == isaret) or
(tahta[9] == isaret and tahta[5] == isaret and tahta[1] == isaret))
def ilk_sirayi_sec():
if random.randint(0, 1) == 0:
return 'Oyuncu 2'
else:
return 'Oyuncu 1'
def bos_mu(tahta, pozisyon):
return tahta[pozisyon] == ' '
def tahta_dolu(tahta):
for i in range(1, 10):
if bos_mu(tahta, i):
return False
return True
def oyuncu_secimi(board):
pozisyon = 0
while pozisyon not in range(1, 10) or not bos_mu(board, pozisyon):
pozisyon = int(input('Bir sonraki pozisyonunuzu seçin: (1-9)'))
return pozisyon
def tekrar_oyna():
return input('Tekrar oynamak istiyor musunuz? E veya H girin: ').lower().startswith('e')
![nwkutjz.jpg](https://i.hizliresim.com/nwkutjz.jpg)