Aula Jazmati
Published © MIT

RPi Tic-Tac-Toe Game with Hexabitz Module Sound Effects

This tutorial will help you play and create Tic-Tac-Toe, a very renowned game we have all got our hands on since our childhood.

IntermediateFull instructions provided1 hour691
RPi Tic-Tac-Toe Game with Hexabitz Module Sound Effects

Things used in this project

Story

Read more

Custom parts and enclosures

Game Voice TIC TAC TOE

Game Voice x

Game Voice o

Tune of Winning

OK

Schematics

Picture of Tools

Code

Hexabitz topology

C/C++
V2.2
/*
    BitzOS (BOS) V0.0.0 - Copyright (C) 2016 Hexabitz
    All rights reserved

    File Name     : topology_1.h
    Description   : Array topology definition.
*/

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __topology_1_H
#define __topology_1_H
#ifdef __cplusplus
 extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "stm32f0xx_hal.h"
	 

#define __N	2				// Number of array modules

// Array modules
#define _mod1	1<<3
#define _mod2	2<<3


// Topology
static uint16_t array[__N][7] = {
{ _H07R3, 0, 0, 0,0,  _mod2|P5, 0},									// Module 1
{ _H1BR6, 0, 0, 0, 0, _mod1|P5, 0},									// Module 2
};

// Configurations for duplex serial ports
#if ( _module == 1 )
	#define	H07R3	1
	#define	_P1pol_normal	1
	#define	_P2pol_normal	1
	#define	_P3pol_normal	1
	#define	_P4pol_normal	1	
	#define	_P5pol_normal	1
	#define	_P6pol_normal	1
#endif
#if ( _module == 2 )
	#define	H1BR6	1
	#define	_P1pol_normal	1
	#define	_P2pol_normal	1
	#define	_P3pol_normal	1
	#define	_P4pol_normal	1	
	#define	_P5pol_reversed	1
	#define	_P6pol_normal	1
#endif

#ifdef __cplusplus
}
#endif
#endif /*__ topology_1_H */


/************************ (C) COPYRIGHT HEXABITZ *****END OF FILE****/

Python game code 1

Python
from tkinter import *
from tkinter import messagebox
import serial
import time
import random as r
ser = serial.Serial(        
               port='/dev/ttyUSB0',
               baudrate = 921600,
               parity=serial.PARITY_NONE,
               stopbits=serial.STOPBITS_ONE,
               bytesize=serial.EIGHTBITS,
               timeout=1
           )
print(ser.name)

ser.write ('\r'.encode())
msg1=ser.read(2000)
print (msg1)
time.sleep(1)
ser.write ('\r'.encode())
msg1=ser.read(2000)
print (msg1)
ser.write ('Play tune d5 c6 f6 g6 a5 c7 c6 b6'.encode())
ser.write ('\r'.encode())
def hexabitz1():
    ser.write ('demo'.encode())
    ser.write ('\r'.encode())
    
def hexabitz2():
    ser.write ('Play tune d5'.encode())
    ser.write ('\r'.encode())
    
def hexabitz3():
    ser.write ('Playfile 2 song1'.encode())
    ser.write ('\r'.encode())
    
def button(frame):          #Function to define a button
    b=Button(frame,padx=1,bg="pink",width=3,text="   ",font=('arial',60,'bold'),relief="sunken",bd=10)
    return b
def change_a():             #Function to change the operand for the next player
    global a
    for i in ['O','X']:
        if not(i==a):
            a=i
            hexabitz2()
            break
def reset():                #Resets the game
    global a
    for i in range(3):
        for j in range(3):
                b[i][j]["text"]=" "
                b[i][j]["state"]=NORMAL
    a=r.choice(['O','X'])
        
def check():                #Checks for victory or Draw
    for i in range(3):
        if(b[i][0]["text"]==b[i][1]["text"]==b[i][2]["text"]==a or b[0][i]["text"]==b[1][i]["text"]==b[2][i]["text"]==a):
            hexabitz1()
            messagebox.showinfo("Congrats!!","'"+a+"' has won")
            time.sleep(2)
            reset()
    if(b[0][0]["text"]==b[1][1]["text"]==b[2][2]["text"]==a or b[0][2]["text"]==b[1][1]["text"]==b[2][0]["text"]==a):
        hexabitz1()
        messagebox.showinfo("Congrats!!","'"+a+"' has won")
        time.sleep(2)
        reset()
    elif(b[0][0]["state"]==b[0][1]["state"]==b[0][2]["state"]==b[1][0]["state"]==b[1][1]["state"]==b[1][2]["state"]==b[2][0]["state"]==b[2][1]["state"]==b[2][2]["state"]==DISABLED):
        hexabitz3()
        messagebox.showinfo("Tied!!","The match ended in a draw")
        reset()
        
        
def click(row,col):
        b[row][col].config(text=a,state=DISABLED,disabledforeground=colour[a])
        check()
        change_a()
        label.config(text=a+"'s Chance")

###############   Main Program #################
root=Tk()                   #Window defined
root.title("Tic-Tac-Toe with Hexabitz")   #Title given
a=r.choice(['O','X'])       #Two operators defined
colour={'O':"deep sky blue",'X':"lawn green"}
b=[[],[],[]]
for i in range(3):
        for j in range(3):
                b[i].append(button(root))
                b[i][j].config(command= lambda row=i,col=j:click(row,col))
                b[i][j].grid(row=i,column=j)
label=Label(text=a+"'s Chance",font=('arial',20,'bold'))
label.grid(row=3,column=0,columnspan=3)
root.mainloop()

Python game code 2

Python
from tkinter import *
from tkinter import messagebox
import serial
import time
import random as r
ser = serial.Serial(        
               port='/dev/ttyUSB0',
               baudrate = 921600,
               parity=serial.PARITY_NONE,
               stopbits=serial.STOPBITS_ONE,
               bytesize=serial.EIGHTBITS,
               timeout=1
           )
print(ser.name)

ser.write ('\r'.encode())
msg1=ser.read(2000)
print (msg1)
time.sleep(1)
ser.write ('playfile 2 s3'.encode())
ser.write ('\r'.encode())
def hexabitz1(): 
    ser.write ('Playfile 2 qq'.encode())
    ser.write ('\r'.encode()) 
def hexabitzx():
    ser.write ('Playfile 2 s1'.encode())
    ser.write ('\r'.encode())
def hexabitzo():
    ser.write ('Playfile 2 s2'.encode())
    ser.write ('\r'.encode())
def hexabitz3():
    ser.write ('Playfile 2 a3'.encode())
    ser.write ('\r'.encode())
    
def button(frame):          #Function to define a button
    b=Button(frame,padx=1,bg="pink",width=3,text="   ",font=('arial',60,'bold'),relief="sunken",bd=10)
    return b
def change_a():             #Function to change the operand for the next player
    global a
    for i in ['X','O']:
        if not(i==a):
            a=i
            break    
        if (a == 'X'):
            hexabitzx()
            time.sleep(1)
        if(a == 'O') :
            hexabitzo()
            time.sleep(1)
        
            
def reset():                #Resets the game
    global a
    for i in range(3):
        for j in range(3):
                b[i][j]["text"]=" "
                b[i][j]["state"]=NORMAL
    a=r.choice(['O','X'])
def check():                #Checks for victory or Draw
    for i in range(3):
            if(b[i][0]["text"]==b[i][1]["text"]==b[i][2]["text"]==a or b[0][i]["text"]==b[1][i]["text"]==b[2][i]["text"]==a):
                    hexabitz1()
                    messagebox.showinfo("Congrats!!","'"+a+"' has won")
                    time.sleep(5)
                    reset()
    if(b[0][0]["text"]==b[1][1]["text"]==b[2][2]["text"]==a or b[0][2]["text"]==b[1][1]["text"]==b[2][0]["text"]==a):
        hexabitz1()
        messagebox.showinfo("Congrats!!","'"+a+"' has won")
        time.sleep(5)
        reset()   
    elif(b[0][0]["state"]==b[0][1]["state"]==b[0][2]["state"]==b[1][0]["state"]==b[1][1]["state"]==b[1][2]["state"]==b[2][0]["state"]==b[2][1]["state"]==b[2][2]["state"]==DISABLED):
        messagebox.showinfo("Tied!!","The match ended in a draw")
        
        reset()
def click(row,col):
        b[row][col].config(text=a,state=DISABLED,disabledforeground=colour[a])
        check()
        
        change_a()
        label.config(text=a+"'s Chance")
###############   Main Program #################
root=Tk()                   #Window defined
root.title("Tic-Tac-Toe with Hexabitz")   #Title given
a=r.choice(['O','X'])       #Two operators defined
colour={'O':"deep sky blue",'X':"lawn green"}
b=[[],[],[]]
for i in range(3):
        for j in range(3):
                b[i].append(button(root))
                b[i][j].config(command= lambda row=i,col=j:click(row,col))
                b[i][j].grid(row=i,column=j)
label=Label(text=a+"'s Chance",font=('arial',20,'bold'))
label.grid(row=3,column=0,columnspan=3)
root.mainloop()

H1BR6x-Firmware

V2.2

H07R3x-Firmware

V2.2

Credits

Aula Jazmati

Aula Jazmati

53 projects • 215 followers
💡🕊️

Comments