After the jumpers have been connected, the entire setup looked like it.
How it Started :I wanted to make something with the Frdm k82f, thought more and ended up in this mind-blowing game tic tac toe. Not only i made it using Frdm I also implemented artificial intelligence to it, to play on its own.
Not much things were needed, just the tools to solder the Led to correct place. About the Led, it is red green led. It contains three pins, the middle pin is ground and the other two pins one act as green and the other one red.
With all set in place, the next step is to solder them. I made it compact. The soldering is to just make every led pins connect to the male headers and use it by means of jumpers to connect to the frdm board. If you want you can make it a shield like thing to frdm board. Maybe it will reduce the jumper wires and makes the device even more compact. I connected every Led ground as common. Used a common Ground.
Connecting the respective Gpio to the respective male header in the tic tac board is tiresome. So used this Comparison methodology to do it easily by seeing the laptop screen.
After the jumpers have been connected, the entire setup looked like this.
The two tasks to do in code is to enable the pins and the ports in the pinmux.c, the pinmux enabling code is given below.
Include files:
#include "fsl_port.h"
#include "fsl_common.h"
#include "pin_mux.h"
Enable Pins:
void BOARD_InitPins(void)
{
CLOCK_EnableClock(kCLOCK_PortC);
CLOCK_EnableClock(kCLOCK_PortB);
CLOCK_EnableClock(kCLOCK_PortA);
CLOCK_EnableClock(kCLOCK_PortD);
PORT_SetPinMux(PORTC, 14U, kPORT_MuxAlt3);
PORT_SetPinMux(PORTC, 15U, kPORT_MuxAlt3);
PORT_SetPinMux(PORTA, 15U, kPORT_MuxAsGpio); //1 R
PORT_SetPinMux(PORTA, 16U, kPORT_MuxAsGpio); //1 G
PORT_SetPinMux(PORTC, 7U, kPORT_MuxAsGpio); //2 R
PORT_SetPinMux(PORTA, 14U, kPORT_MuxAsGpio); //2 G
PORT_SetPinMux(PORTA, 17U, kPORT_MuxAsGpio); //3 R
PORT_SetPinMux(PORTA, 12U, kPORT_MuxAsGpio); //3 G
PORT_SetPinMux(PORTA, 13U, kPORT_MuxAsGpio); //4 R
PORT_SetPinMux(PORTA, 5U, kPORT_MuxAsGpio); //4 G
PORT_SetPinMux(PORTB, 23U, kPORT_MuxAsGpio); //5 R
PORT_SetPinMux(PORTB, 22U, kPORT_MuxAsGpio); //5 G
PORT_SetPinMux(PORTB, 21U, kPORT_MuxAsGpio); //6 R
PORT_SetPinMux(PORTB, 20U, kPORT_MuxAsGpio); //6 G
PORT_SetPinMux(PORTC, 12U, kPORT_MuxAsGpio); //7 R
PORT_SetPinMux(PORTB, 17U, kPORT_MuxAsGpio); //7 G
PORT_SetPinMux(PORTB, 16U, kPORT_MuxAsGpio); //8 R
PORT_SetPinMux(PORTC, 11U, kPORT_MuxAsGpio); //8 G
PORT_SetPinMux(PORTC, 5U, kPORT_MuxAsGpio); //9 R
PORT_SetPinMux(PORTC, 3U, kPORT_MuxAsGpio); //9 G
}
The next setup in coding is to give functionality to this pins in the main code. The main code accepts the value from the user and lights up the corresponding colour LED. Code is given below.
Include statements and definitions:
#include "fsl_debug_console.h"
#include "fsl_gpio.h"
#include "board.h"
#include "clock_config.h"
#include "pin_mux.h"
#include <stdlib.h>
#include <stdio.h>
#define High 1
static char g_StrMenu[] =
"\r\n"
"#################\r\n"
"## 1 ## 2 ## 3 ##\r\n"
"## 4 ## 5 ## 6 ##\r\n"
"## 7 ## 8 ## 9 ##\r\n"
"#################\r\n"
"\r\n"
"\r\n1 Red a 1 Green b"
"\r\n2 Red c 2 Green d"
"\r\n3 Red e 3 Green f"
"\r\n4 Red g 4 Green h"
"\r\n5 Red i 5 Green j"
"\r\n6 Red k 6 Green l"
"\r\n7 Red m 7 Green n"
"\r\n8 Red o 8 Green p"
"\r\n9 Red q 9 Green r";
static char g_StrNewline[] = "\r\n";
Case Structure:
int main(void)
{
uint8_t index;
gpio_pin_config_t led_config = {
kGPIO_DigitalOutput, 0,
};
/* Board pin, clock, debug console init */
BOARD_InitPins();
BOARD_BootClockRUN();
BOARD_InitDebugConsole();
GPIO_PinInit(GPIOA, 15U, &led_config); //1 R a
GPIO_PinInit(GPIOA, 16U, &led_config); //1 G b
GPIO_PinInit(GPIOC, 7U, &led_config); //2 R c
GPIO_PinInit(GPIOA, 14U, &led_config); //2 G d
GPIO_PinInit(GPIOA, 17U, &led_config); //3 R e
GPIO_PinInit(GPIOA, 12U, &led_config); //3 G f
GPIO_PinInit(GPIOA, 13U, &led_config); //4 R g
GPIO_PinInit(GPIOA, 5U, &led_config); //4 G h
GPIO_PinInit(GPIOB, 23U, &led_config); //5 R i
GPIO_PinInit(GPIOB, 22U, &led_config); //5 G j
GPIO_PinInit(GPIOB, 21U, &led_config); //6 R k
GPIO_PinInit(GPIOB, 20U, &led_config); //6 G l
GPIO_PinInit(GPIOC, 12U, &led_config); //7 R m
GPIO_PinInit(GPIOB, 17U, &led_config); //7 G n
GPIO_PinInit(GPIOB, 16U, &led_config); //8 R o
GPIO_PinInit(GPIOC, 11U, &led_config); //8 G p
GPIO_PinInit(GPIOC, 5U, &led_config); //9 R q
GPIO_PinInit(GPIOC, 3U, &led_config); //9 G r
while (1)
{
PRINTF(g_StrMenu);
PRINTF("\r\nSelect:");
index = GETCHAR();
PUTCHAR(index);
PRINTF(g_StrNewline);
switch (index)
{
case 'a':
GPIO_WritePinOutput (GPIOA, 15U, High);
break;
case 'b':
GPIO_WritePinOutput (GPIOA, 16U, High);
break;
case 'c':
GPIO_WritePinOutput (GPIOC, 7U, High);
break;
case 'd':
GPIO_WritePinOutput (GPIOA, 14U, High);
break;
case 'e':
GPIO_WritePinOutput (GPIOA, 17U, High);
break;
case 'f':
GPIO_WritePinOutput (GPIOA, 12U, High);
break;
case 'g':
GPIO_WritePinOutput (GPIOA, 13U, High);
break;
case 'h':
GPIO_WritePinOutput (GPIOA, 5U, High);
break;
case 'i':
GPIO_WritePinOutput (GPIOB, 23U, High);
break;
case 'j':
GPIO_WritePinOutput (GPIOB, 22U, High);
break;
case 'k':
GPIO_WritePinOutput (GPIOB, 21U, High);
break;
case 'l':
GPIO_WritePinOutput (GPIOB, 20U, High);
break;
case 'm':
GPIO_WritePinOutput (GPIOC, 12U, High);
break;
case 'n':
GPIO_WritePinOutput (GPIOB, 17U, High);
break;
case 'o':
GPIO_WritePinOutput (GPIOB, 16U, High);
break;
case 'p':
GPIO_WritePinOutput (GPIOC, 11U, High);
break;
case 'q':
GPIO_WritePinOutput (GPIOC, 5U, High);
break;
case 'r':
GPIO_WritePinOutput (GPIOC, 3U, High);
break;
default:
break;
}
}
}
Video Pictures:Two player game was easy to build, I made it and used the boolean conditions below to check the status of who won the game. The Boolean code is available below.
bool gameover()
{
for (int i = 0; i < 3; i++)//Check for a win
{
if ((board[i][0] == board[i][1] && board[i][1] == board[i][2]) || (board[0][i] == board[1][i] && board[1][i] == board[2][i]) || (board[0][0] == board[1][1] && board[1][1] == board[2][2]) || (board[0][2] == board[1][1] && board[1][1] == board[2][0]))
{
return true;
}
}
for (int i = 0; i < 3; i++)//Check for draw
{
for (int j = 0; j < 3; j++)
{
if (board[i][j] != 'X' && board[i][j] != 'O')
{
return false;
}
}
}
draw = true;
return true;
}
Player vs Computer:But then thought of implementing the single player by means of artificial intelligence code. Wrote a necessary condition statement codes, and made the computer play in different difficulty stages in each level.
I have written the code in C++ and made changes to it and implemented in Kds IDE. There are a lot of ways where you can code the AI part. For the AI part, for now I have used random function.
#include <iostream.h>
#include <conio.h>
using namespace std;
int checkwin( char[]);
void board( char[]);
void main()
{
char square[10] = {'o','1','2','3','4','5','6','7','8','9'};
int player = 1,i,choice;
char mark;
do
{
board(square);
if(player%2==1)
player=1;
else
player=2;
// player 2
if(player==2)
{
cout << "Players " << player<
cout << "Press Enter for CPU"<
choice=rand()%9;
mark='O';
int turn=1;
int placed=0;
while(placed==0)
{
if (choice == 1 && square[1] == '1'){
square[1] = mark;
placed=1;
}
else if (choice == 2 && square[2] == '2'){
square[2] = mark;
placed=1;
}
else if (choice == 3 && square[3] == '3'){
square[3] = mark;
placed=1;
}
else if (choice == 4 && square[4] == '4'){
placed=1;
square[4] = mark;
}
else if (choice == 5 && square[5] == '5'){
square[5] = mark;
placed=1;
}
else if (choice == 6 && square[6] == '6'){
square[6] = mark;
placed=1;
}
else if (choice == 7 && square[7] == '7'){
square[7] = mark;
placed=1;
}
else if (choice == 8 && square[8] == '8'){
square[8] = mark;
placed=1;
}
else if (choice == 9 && square[9] == '9'){
square[9] = mark;
placed=1;
}
else
{
choice=rand()%9;
}
}
i=checkwin(square);
player++;
_getche();
board(square);
}
// player 1
else if(player==1)
{
cout << "Players " << player << ", enter a number: ";
cin >> choice;
mark='X';
if (choice == 1 && square[1] == '1')
square[1] = mark;
else if (choice == 2 && square[2] == '2')
square[2] = mark;
else if (choice == 3 && square[3] == '3')
square[3] = mark;
else if (choice == 4 && square[4] == '4')
square[4] = mark;
else if (choice == 5 && square[5] == '5')
square[5] = mark;
else if (choice == 6 && square[6] == '6')
square[6] = mark;
else if (choice == 7 && square[7] == '7')
square[7] = mark;
else if (choice == 8 && square[8] == '8')
square[8] = mark;
else if (choice == 9 && square[9] == '9')
square[9] = mark;
else
{
cout<<"Invalid move ";
player--;
_getche();
}
i=checkwin(square);
player++;
}
}while(i==-1);
board(square);
if(i==1)
cout<<"Congratulation! \nPlayer "<<--player<<" win ";
else
cout<<" OOps!\nGame draw";
_getche();
}
void board(char square[])
{
system("cls");
cout << "\n\n\tTic Tac Toe\n\n";
cout << "Player 1 (X) - Player 2 (O)" << endl << endl;
cout << endl;
cout << " | | " << endl;
cout << " " << square[1] << " | " << square[2] << " | " << square[3] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << square[4] << " | " << square[5] << " | " << square[6] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << square[7] << " | " << square[8] << " | " << square[9] << endl;
cout << " | | " << endl << endl;
}
int checkwin(char square[])
{
if (square[1] == square[2] && square[2] == square[3])
return 1;
else if (square[4] == square[5] && square[5] == square[6])
return 1;
else if (square[7] == square[8] && square[8] == square[9])
return 1;
else if (square[1] == square[4] && square[4] == square[7])
return 1;
else if (square[2] == square[5] && square[5] == square[8])
return 1;
else if (square[3] == square[6] && square[6] == square[9])
return 1;
else if (square[1] == square[5] && square[5] == square[9])
return 1;
else if (square[3] == square[5] && square[5] == square[7])
return 1;
else if (square[1] != '1' && square[2] != '2' && square[3] != '3'
&& square[4] != '4' && square[5] != '5' && square[6] != '6'
&& square[7] != '7' && square[8] != '8' && square[9] != '9')
return 0;
else
return -1;
}
Why I did this:A simple module to play with friends, that was the idea I got in mind. Then searched for some games. Liked the idea of doing TicTacToe game in a simple module. And FRDM k82f had enormous pins that I can use to light up the desired Leds. Thank you so much !
Comments