NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

# Importing required moduless
import numpy as np
import pandas as pd
import pprint,random

from scipy.ndimage.interpolation import shift
import matplotlib.pyplot as plt

from keras.models import Sequential
from keras.layers import Dense,Dropout
from keras.optimizers import SGD

class tic_tac_toe_game(object):
def __init__(self):
self.board=np.full((3,3),2)

def toss(self):
turn=np.random.randint(0,2,size=1)
if turn.mean()==0:
self.turn_monitor=0
elif turn.mean()==1:
self.turn_monitor=1
return self.turn_monitor

def move(self,player,coord):
if self.board[coord]!=2 or self.game_status()!="In Progress" or self.turn_monitor!=player:
raise ValueError("Invalid move")
self.board[coord]=player
self.turn_monitor=1-player
return self.game_status(),self.board


def game_status(self):
#check for a win along rows
for i in range(self.board.shape[0]):
if 2 not in self.board[i,:] and len(set(self.board[i,:]))==1:
return "Won"
#check for a win along columns
for j in range(self.board.shape[1]):
if 2 not in self.board[:,j] and len(set(self.board[:,j]))==1:
return "Won"
# check for a win along diagonals
if 2 not in np.diag(self.board) and len(set(np.diag(self.board)))==1:
return "Won"
if 2 not in np.diag(np.fliplr(self.board)) and len(set(np.diag(np.fliplr(self.board))))==1:
return "Won"
# check for a Draw
if not 2 in self.board:
return "Drawn"
else:
return "In Progress"
def legal_moves_generator(current_board_state,turn_monitor):
legal_moves_dict={}
for i in range(current_board_state.shape[0]):
for j in range(current_board_state.shape[1]):
if current_board_state[i,j]==2:
board_state_copy=current_board_state.copy()
board_state_copy[i,j]=turn_monitor
legal_moves_dict[(i,j)]=board_state_copy.flatten()
return legal_moves_dict
model = Sequential()
model.add(Dense(18, input_dim=9,kernel_initializer='normal', activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(9, kernel_initializer='normal',activation='relu'))
model.add(Dropout(0.1))
# model.add(Dense(9, kernel_initializer='normal',activation='relu'))
# model.add(Dropout(0.1))
# model.add(Dense(5, kernel_initializer='normal',activation='relu'))
model.add(Dense(1,kernel_initializer='normal'))

learning_rate = 0.001
momentum = 0.8

sgd = SGD(lr=learning_rate, momentum=momentum,nesterov=False)
model.compile(loss='mean_squared_error', optimizer=sgd)
model.summary()
# create an object of the class tick_tac_toe_game
game=tic_tac_toe_game()
# toss to decide which player goes first
game.toss()
print("Player ",game.turn_monitor," has won the toss")
# make the first move
print("Initial board state n",game.board)
print("Let first player place their mark on 0,0")
game_status,board=game.move(game.turn_monitor,(0,0))
print("New Board State: n",board)
print("Let second player place their mark on 0,1")
game_status,board=game.move(game.turn_monitor,(0,1))
print("New Board State: n",board)
print("Let first player place their mark on 1,1")
game_status,board=game.move(game.turn_monitor,(1,1))
print("New Board State: n",board)
print("Let second player place their mark on 0,2")
game_status,board=game.move(game.turn_monitor,(0,2))
print("New Board State: n",board)
print("Let first player place their mark on 2,2")
game_status,board=game.move(game.turn_monitor,(2,2))
print("New Board State: n",board)
print("Player ",1-game.turn_monitor," Has ",game_status)
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.