onTimer not working as intended - Vector CANoe - timer

I am using a DEMO version of Vector CANoe 15.1 and for now, I've managed to create two nodes and two panels. I've created some buttons with which I can control some LEDs. The idea is that I want to use timers so that when I press a button, my LED is blinking with a frequency of 1Hz(1sec). I managed to make it work in 3 out of 5 IGNITION States, but not in the other 2, even if I used the same code. Maybe if I show you my code you can understand me better.
/*#!Encoding:1252*/
includes
{
}
variables
{
msTimer MainCyclic;
msTimer TurnLeftCyclic;
msTimer TurnRightCyclic;
msTimer HazardCyclic;
int ignition;
int hazardWarning;
int turnlights;
int flag1;
int flag2;
int flag3;
byte byte0;
}
on preStart
{
flag1=0;
flag2=0;
flag3=0;
}
on start
{
setTimer(MainCyclic,50);
// setTimer(TurnLeftCyclic,1500);
// setTimer(TurnRightCyclic,1500);
// setTimer(HazardCyclic,1000);
}
on timer MainCyclic
{
setTimer(MainCyclic,50);
fMain();
}
on timer TurnLeftCyclic
{
setTimer(TurnLeftCyclic,1500);
if (#IPC::LeftSignal == 1)
{
#IPC::LeftSignal=0;
#IPC::RightSignal=0;
}
else
{
#IPC::LeftSignal=1;
#IPC::RightSignal=0;
}
}
on timer TurnRightCyclic
{
setTimer(TurnRightCyclic,1500);
if (#IPC::RightSignal == 1)
{
#IPC::RightSignal=0;
#IPC::LeftSignal=0;
}
else
{
#IPC::RightSignal=1;
#IPC::LeftSignal=0;
}
}
on timer HazardCyclic
{
setTimer(HazardCyclic,1000);
if (#IPC::LeftSignal == 1 && #IPC::RightSignal == 1)
{
#IPC::LeftSignal=0;
#IPC::RightSignal=0;
}
else if (#IPC::LeftSignal == 0 && #IPC::RightSignal == 0)
{
#IPC::LeftSignal=1;
#IPC::RightSignal=1;
}
}
on message ExternalLights
{
byte0=this.byte(0);
if (byte0 == 0x0)
{
turnlights=0;
hazardWarning=0;
}
if (byte0 == 0x1)
{
turnlights=1;
hazardWarning=0;
}
if (byte0 == 0x2)
{
turnlights=2;
hazardWarning=0;
}
if (byte0 == 0x4)
{
turnlights=0;
hazardWarning=1;
}
if (byte0 == 0x5)
{
turnlights=1;
hazardWarning=1;
}
if (byte0 == 0x6)
{
turnlights=2;
hazardWarning=1;
}
}
on message IGN
{
byte0=this.byte(0);
if (byte0 == 0x0)
ignition=0;
if (byte0 == 0x1)
ignition=1;
if (byte0 == 0x2)
ignition=2;
if (byte0 == 0x3)
ignition=3;
if (byte0 == 0x4)
ignition=4;
}
void fMain()
{
if (ignition == 0 || ignition == 1)
{
if (turnlights == 0 || turnlights == 1 || turnlights == 2)
{
#IPC::LeftSignal=0;
#IPC::RightSignal=0;
flag1=0;
flag2=0;
flag3=0;
cancelTimer(TurnLeftCyclic);
cancelTimer(TurnRightCyclic);
}
if (hazardWarning == 1 && flag3==0)
{
#IPC::LeftSignal=1;
#IPC::RightSignal=1;
setTimer(HazardCyclic,1000);
flag1=0;
flag2=0;
flag3=1;
cancelTimer(TurnLeftCyclic);
cancelTimer(TurnRightCyclic);
}
else
{
#IPC::LeftSignal=0;
#IPC::RightSignal=0;
flag1=0;
flag2=0;
flag3=0;
cancelTimer(TurnLeftCyclic);
cancelTimer(TurnRightCyclic);
cancelTimer(HazardCyclic);
}
}
else if (ignition == 2 || ignition == 3)
{
if (hazardWarning == 0)
{
if (turnlights == 0)
{
#IPC::LeftSignal = 0;
#IPC::RightSignal = 0;
flag1=0;
flag2=0;
flag3=0;
cancelTimer(TurnLeftCyclic);
cancelTimer(TurnRightCyclic);
cancelTimer(HazardCyclic);
}
else if (turnlights == 1 && flag1==0)
{
#IPC::LeftSignal = 1;
#IPC::RightSignal = 0;
setTimer(TurnLeftCyclic,1500);
flag1=1;
flag2=0;
flag3=0;
cancelTimer(TurnRightCyclic);
cancelTimer(HazardCyclic);
}
else if (turnlights == 2 && flag2==0)
{
#IPC::LeftSignal = 0;
#IPC::RightSignal = 1;
setTimer(TurnRightCyclic,1500);
flag1=0;
flag2=1;
flag3=0;
cancelTimer(TurnLeftCyclic);
cancelTimer(HazardCyclic);
}
}
else if (hazardWarning==1 && flag3==0)
{
#IPC::LeftSignal = 1;
#IPC::RightSignal = 1;
setTimer(HazardCyclic,1000);
flag1=0;
flag2=0;
flag3=1;
cancelTimer(TurnLeftCyclic);
cancelTimer(TurnRightCyclic);
}
}
else if (ignition == 4 && flag3==0)
{
#IPC::LeftSignal=1;
#IPC::RightSignal=1;
setTimer(HazardCyclic,1000);
flag1=0;
flag2=0;
flag3=1;
cancelTimer(TurnLeftCyclic);
cancelTimer(TurnRightCyclic);
}
}
When ignition=0 or 1, should only work as hazardLights(both left and right blinking in the same time). The problem is that when I press the HazardSwitch, LEDs turn ON, but not blinking. I will attach a photo that contains a graphic. For the first seconds, is how this should work(as it works in ignition=2 or 3 or 4) and after that you can see how it works when ignition=0 or 1.
My panels + graphic

I`ve found the problem. Conditions for Ignition 0 or 1 were not set correctly and flag3 was reseted too many times so that my HazardCyclic timer was turning on and off as long as my switch was 0x1. Here is the code now:
includes
{
}
variables
{
msTimer MainCyclic;
msTimer TurnLeftCyclic;
msTimer TurnRightCyclic;
msTimer HazardCyclic;
int ignition;
int hazardWarning;
int turnlights;
int flag1;
int flag2;
int flag3;
byte byte0;
}
on preStart
{
flag1=0;
flag2=0;
flag3=0;
}
on start
{
setTimer(MainCyclic,50);
// setTimer(TurnLeftCyclic,1500);
// setTimer(TurnRightCyclic,1500);
// setTimer(HazardCyclic,1000);
}
on timer MainCyclic
{
setTimer(MainCyclic,50);
fMain();
}
on timer TurnLeftCyclic
{
setTimer(TurnLeftCyclic,1500);
if (#IPC::LeftSignal == 1)
{
#IPC::LeftSignal=0;
#IPC::RightSignal=0;
}
else
{
#IPC::LeftSignal=1;
#IPC::RightSignal=0;
}
}
on timer TurnRightCyclic
{
setTimer(TurnRightCyclic,1500);
if (#IPC::RightSignal == 1)
{
#IPC::RightSignal=0;
#IPC::LeftSignal=0;
}
else
{
#IPC::RightSignal=1;
#IPC::LeftSignal=0;
}
}
on timer HazardCyclic
{
setTimer(HazardCyclic,1000);
if (#IPC::LeftSignal == 1 && #IPC::RightSignal == 1)
{
#IPC::LeftSignal=0;
#IPC::RightSignal=0;
}
else if (#IPC::LeftSignal == 0 && #IPC::RightSignal == 0)
{
#IPC::LeftSignal=1;
#IPC::RightSignal=1;
}
}
on message ExternalLights
{
byte0=this.byte(0);
if (byte0 == 0x0)
{
turnlights=0;
hazardWarning=0;
}
if (byte0 == 0x1)
{
turnlights=1;
hazardWarning=0;
}
if (byte0 == 0x2)
{
turnlights=2;
hazardWarning=0;
}
if (byte0 == 0x4)
{
turnlights=0;
hazardWarning=1;
}
if (byte0 == 0x5)
{
turnlights=1;
hazardWarning=1;
}
if (byte0 == 0x6)
{
turnlights=2;
hazardWarning=1;
}
}
on message IGN
{
byte0=this.byte(0);
if (byte0 == 0x0)
ignition=0;
if (byte0 == 0x1)
ignition=1;
if (byte0 == 0x2)
ignition=2;
if (byte0 == 0x3)
ignition=3;
if (byte0 == 0x4)
ignition=4;
}
void fMain()
{
if (ignition == 0 || ignition == 1)
{
if ((turnlights == 0 || turnlights == 1 || turnlights == 2) && (hazardWarning == 0))
{
#IPC::LeftSignal=0;
#IPC::RightSignal=0;
flag1=0;
flag2=0;
flag3=0;
cancelTimer(TurnLeftCyclic);
cancelTimer(TurnRightCyclic);
}
if (hazardWarning == 1 && flag3==0)
{
#IPC::LeftSignal=1;
#IPC::RightSignal=1;
setTimer(HazardCyclic,1000);
flag1=0;
flag2=0;
flag3=1;
cancelTimer(TurnLeftCyclic);
cancelTimer(TurnRightCyclic);
}
else if(hazardWarning == 0)
{
#IPC::LeftSignal=0;
#IPC::RightSignal=0;
flag1=0;
flag2=0;
flag3=0;
cancelTimer(TurnLeftCyclic);
cancelTimer(TurnRightCyclic);
cancelTimer(HazardCyclic);
}
}
else if (ignition == 2 || ignition == 3)
{
if (hazardWarning == 0)
{
if (turnlights == 0)
{
#IPC::LeftSignal = 0;
#IPC::RightSignal = 0;
flag1=0;
flag2=0;
flag3=0;
cancelTimer(TurnLeftCyclic);
cancelTimer(TurnRightCyclic);
cancelTimer(HazardCyclic);
}
else if (turnlights == 1 && flag1==0)
{
#IPC::LeftSignal = 1;
#IPC::RightSignal = 0;
setTimer(TurnLeftCyclic,1500);
flag1=1;
flag2=0;
flag3=0;
cancelTimer(TurnRightCyclic);
cancelTimer(HazardCyclic);
}
else if (turnlights == 2 && flag2==0)
{
#IPC::LeftSignal = 0;
#IPC::RightSignal = 1;
setTimer(TurnRightCyclic,1500);
flag1=0;
flag2=1;
flag3=0;
cancelTimer(TurnLeftCyclic);
cancelTimer(HazardCyclic);
}
}
else if (hazardWarning==1 && flag3==0)
{
#IPC::LeftSignal = 1;
#IPC::RightSignal = 1;
setTimer(HazardCyclic,1000);
flag1=0;
flag2=0;
flag3=1;
cancelTimer(TurnLeftCyclic);
cancelTimer(TurnRightCyclic);
}
}
else if (ignition == 4 && flag3==0)
{
#IPC::LeftSignal=1;
#IPC::RightSignal=1;
setTimer(HazardCyclic,1000);
flag1=0;
flag2=0;
flag3=1;
cancelTimer(TurnLeftCyclic);
cancelTimer(TurnRightCyclic);
}
}
Thank you!

Related

How can I make this programe about the game of 15 work?

I'm trying to do a programme in C, that should allow you to play the game of 15 (in this game you have a 4x4 bracket with 15 dowels and there's an empty space where you have to move the dowel you want; these dowels are in random position so the objective of the game is putting all the dowels in the right order from 1 to 15 like in the image attached)
https://it.wikipedia.org/wiki/Gioco_del_quindici#/media/File:15-puzzle.svg --> link of the image
So my problem is: when I have to move a dowel to a corner box (e.g. left top, right top, bottom left, bottom right) the programme doesn't move the dowel that I've decided to move. What's wrong?
Thank you so much
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int x[16];
int max = 16, min = 1, count = 0, mossa, scambio, i, k = 0;
srand(time(NULL));
printf("Benvenuto nel gioco del 15!\n\n");
for (int i = 0; i < 16; i++) {
int j = 0;
do {
x[i]= (rand() % (max-min+1)) + min;
j = 0;
while ( j != i && x[i] != x[j] ) ++j;
} while ( j != i );
}
for (int i = 0; i < 4; i++) {
printf(" | | | |\n");
if (x[count] > 9) {
if (x[count] == 16) {
printf("_ _|");
}
else {
printf("%d_|", x[count]);
}
}
else {
printf("_%d_|", x[count]);
}
if (x[count+1] > 9) {
if (x[count+1] == 16) {
printf("_ _|");
}
else {
printf("%d_|", x[count+1]);
}
}
else {
printf("_%d_|", x[count+1]);
}
if (x[count+2] > 9) {
if (x[count+2] == 16) {
printf("_ _|");
}
else {
printf("%d_|", x[count+2]);
}
}
else {
printf("_%d_|", x[count+2]);
}
if (x[count+3] > 9) {
if (x[count+3] == 16) {
printf("_ _|");
}
else {
printf("%d_|\n", x[count+3]);
}
}
else {
printf("_%d_|\n", x[count+3]);
}
count+=4;
}
while (x[0] != 1 || x[1] != 2 || x[2] != 3 || x[3] != 4 || x[4] != 5 || x[5] != 6 || x[6] != 7 || x[7] != 8 || x[8] != 9 || x[9] != 10 || x[10] != 11 || x[11] != 12 ||x[12] != 13 ||x[13] != 14 ||x[14] != 15 ||x[15] != 16) {
count = 0;
printf("\n\nInsersci quale numero desideri spostare nello spazio vuoto: ");
scanf("%d", &mossa);
system("cls");
for (int i = 0; i < 16; i++) {
if (x[i] == 16) {
if (i == 5 || i == 6 || i == 9 || i == 10) {
if (mossa == x[i-4]) {
scambio = x[i];
x[i] = x[i-4];
x[i-4] = scambio;
}
else if (mossa == x[i+4]) {
scambio = x[i];
x[i] = x[i+4];
x[i+4] = scambio;
}
else if (mossa == x[i-1]) {
scambio = x[i];
x[i] = x[i-1];
x[i-1] = scambio;
}
else if (mossa == x[i+1]) {
scambio = x[i];
x[i] = x[i+1];
x[i+1] = scambio;
}
else {
printf("\nMossa non valida!\n");
}
}
else if (i == 1 || i == 2) {
if (mossa == x[i-1]) {
scambio = x[i];
x[i] = x[i-1];
x[i-1] = scambio;
}
else if (mossa == x[i+1]) {
scambio = x[i];
x[i] = x[i+1];
x[i+1] = scambio;
}
else if (mossa = x[i+4]) {
scambio = x[i];
x[i] = x[i+4];
x[i+4] = scambio;
}
else {
printf("\nMossa non valida!\n");
}
}
else if (i == 4 || i == 8) {
if (mossa = x[i+1]) {
scambio = x[i];
x[i] = x[i+1];
x[i+1] = scambio;
}
else if (mossa = x[i+4]) {
scambio = x[i];
x[i] = x[i+4];
x[i+4] = scambio;
}
else if (mossa == x[i-4]) {
scambio = x[i];
x[i] = x[i-4];
x[i-4] = scambio;
}
else {
printf("\nMossa non valida!\n");
}
}
else if (i == 7 || i == 11) {
if (mossa = x[i-1]) {
scambio = x[i];
x[i] = x[i-1];
x[i-1] = scambio;
}
else if (mossa = x[i+4]) {
scambio = x[i];
x[i] = x[i+4];
x[i+4] = scambio;
}
else if (mossa == x[i-4]) {
scambio = x[i];
x[i] = x[i-4];
x[i-4] = scambio;
}
else {
printf("\nMossa non valida!\n");
}
}
else if (i == 13 || i == 14) {
if (mossa = x[i-1]) {
scambio = x[i];
x[i] = x[i-1];
x[i-1] = scambio;
}
else if (mossa = x[i+1]) {
scambio = x[i];
x[i] = x[i+1];
x[i+1] = scambio;
}
else if (mossa == x[i-4]) {
scambio = x[i];
x[i] = x[i-4];
x[i-4] = scambio;
}
else {
printf("\nMossa non valida!\n");
}
}
else if (i == 0) {
if (mossa = x[i+1]) {
scambio = x[i];
x[i] = x[i+1];
x[i+1] = scambio;
}
else if (mossa = x[i+4]) {
scambio = x[i];
x[i] = x[i+4];
x[i+4] = scambio;
}
else {
printf("\nMossa non valida!\n");
}
}
else if (i == 3) {
if (mossa = x[i-1]) {
scambio = x[i];
x[i] = x[i-1];
x[i-1] = scambio;
}
else if (mossa = x[i+4]) {
scambio = x[i];
x[i] = x[i+4];
x[i+4] = scambio;
}
else {
printf("\nMossa non valida!\n");
}
}
else if (i == 12) {
if (mossa = x[i+1]) {
scambio = x[i];
x[i] = x[i+1];
x[i+1] = scambio;
}
else if (mossa == x[i-4]) {
scambio = x[i];
x[i] = x[i-4];
x[i-4] = scambio;
}
else {
printf("\nMossa non valida!\n");
}
}
else {
if (mossa = x[i-1]) {
scambio = x[i];
x[i] = x[i-1];
x[i-1] = scambio;
}
else if (mossa == x[i-4]) {
scambio = x[i];
x[i] = x[i-4];
x[i-4] = scambio;
}
else {
printf("\nMossa non valida!\n");
}
}
break;
}
}
for (int i = 0; i < 4; i++) {
printf(" | | | |\n");
if (x[count] > 9) {
if (x[count] == 16) {
printf("_ _|");
}
else {
printf("%d_|", x[count]);
}
}
else {
printf("_%d_|", x[count]);
}
if (x[count+1] > 9) {
if (x[count+1] == 16) {
printf("_ _|");
}
else {
printf("%d_|", x[count+1]);
}
}
else {
printf("_%d_|", x[count+1]);
}
if (x[count+2] > 9) {
if (x[count+2] == 16) {
printf("_ _|");
}
else {
printf("%d_|", x[count+2]);
}
}
else {
printf("_%d_|", x[count+2]);
}
if (x[count+3] > 9) {
if (x[count+3] == 16) {
printf("_ _|");
}
else {
printf("%d_|\n", x[count+3]);
}
}
else {
printf("_%d_|\n", x[count+3]);
}
count+=4;
}
k++;
}
printf("\n\nComplimenti hai vinto!");
printf("\nHai realizzato %d mosse", k);
return 0;
}

3x3 Tic-Tac-Toe in C using Minimax

Note #1 - There is a similar question but that is in Python, and I can't figure out this in C.
Note #2 - This is a human vs. AI game and I will call the AI as 'cpu'. The cpu symbol is 'O' and human symbol is 'X' always.
Note #3 - I want to design the game so that the cpu never loses (either win or draw).
I want the user to go first and choose any of the 9 squares. So basically I want the cpu to brute force calculate the result of every way the game can go, and based on that assign 'scores' to its possible moves (8 remaining choices). This way it backtracks and knows which way to go so as to not lose.
Also I want to do that using the minimax algorithm, and in C.
My problem - What I think is that I'm missing a function that actually makes use of the returned 'scores'. I do not want the code, but I would appreciate it if I could get an idea of how to implement that function. Also the nextMove() function confuses me, I want it to check if there is no unfilled box, then fill it with 'O' and then it's the player's turn. But since it will be called again in the recursion, it might not work correctly. Any suggestions?
Edit - The bounty goes to the answer that guides me on how to implement this game using minimax algorithm as described here: https://en.wikipedia.org/wiki/Minimax#Pseudocode
#include <stdio.h>
#define FALSE 0
#define TRUE 1
#define NEGINF -10000
#define POSINF +10000
struct node { // each node is like a snapshot of the game at that point in time
char board[9]; // the 3x3 square is implemented as an array of 9 chars
int value; // the value of that snapshot (+1 if cpu wins, -1 if cpu loses, 0 for draw)
int position; // 0 to 8 inclusive with 0 being [0][0] and 8 being [2][2] (row major fashion) and indicates position where the next 'X' or 'O' would be assigned
};
int max(int value, int returnedValue) {
return value > returnedValue ? value : returnedValue;
}
int min(int value, int returnedValue) {
return value < returnedValue ? value : returnedValue;
}
int someoneHasWon(struct node someNode) {
if(someNode.board[0] == someNode.board[3] && someNode.board[3] == someNode.board[6] && someNode.board[6] == 'X')
return TRUE;
if(someNode.board[0] == someNode.board[3] && someNode.board[3] == someNode.board[6] && someNode.board[6] == 'O')
return TRUE;
//someNode.value = -1;
if(someNode.board[1] == someNode.board[4] && someNode.board[4] == someNode.board[7] && someNode.board[7] == 'X')
return TRUE;
//someNode.value = 1;//return 1;
if(someNode.board[1] == someNode.board[4] && someNode.board[4] == someNode.board[7] && someNode.board[7] == 'O')
return TRUE;
//someNode.value = -1;//return -1;
if(someNode.board[2] == someNode.board[5] && someNode.board[5] == someNode.board[8] && someNode.board[8] == 'X')
return TRUE;
//someNode.value = 1;//return 1;
if(someNode.board[2] == someNode.board[5] && someNode.board[5] == someNode.board[8] && someNode.board[8] == 'O')
return TRUE;
//someNode.value = -1;//return -1;
if(someNode.board[0] == someNode.board[1] && someNode.board[1] == someNode.board[2] && someNode.board[2] == 'X')
return TRUE;
////someNode.value = 1;//return 1;
if(someNode.board[0] == someNode.board[1] && someNode.board[1] == someNode.board[2] && someNode.board[2] == 'O')
return TRUE;
//someNode.value = -1;//return -1;
if(someNode.board[3] == someNode.board[4] && someNode.board[4] == someNode.board[5] && someNode.board[5] == 'X')
return TRUE;
//someNode.value = 1;//return 1;
if(someNode.board[3] == someNode.board[4] && someNode.board[4] == someNode.board[5] && someNode.board[5] == 'O')
return TRUE;
//someNode.value = -1;//return -1;
if(someNode.board[6] == someNode.board[7] && someNode.board[7] == someNode.board[8] && someNode.board[8] == 'X')
return TRUE;
//someNode.value = 1;//return 1;
if(someNode.board[6] == someNode.board[7] && someNode.board[7] == someNode.board[8] && someNode.board[8] == 'O')
return TRUE;
//someNode.value = -1;//return -1;
if(someNode.board[0] == someNode.board[4] && someNode.board[4] == someNode.board[8] && someNode.board[8] == 'X')
return TRUE;
//someNode.value = 1;//return 1;
if(someNode.board[0] == someNode.board[4] && someNode.board[4] == someNode.board[8] && someNode.board[8] == 'O')
return TRUE;
//someNode.value = -1;//return -1;
if(someNode.board[6] == someNode.board[4] && someNode.board[4] == someNode.board[2] && someNode.board[2] == 'X')
return TRUE;
//someNode.value = 1;//return 1;
if(someNode.board[6] == someNode.board[4] && someNode.board[4] == someNode.board[2] && someNode.board[2] == 'O')
return TRUE;
return FALSE;
}
int isTerminal(struct node someNode) {
for(int i = 0; i < 9; i++)
if(someNode.board[i] == ' ') // if square left to fill then game is yet incomplete
return FALSE;
if(someoneHasWon(someNode) == TRUE) // if any player has won earlier with squares left
return TRUE;
return TRUE; // if it's a draw with no squares left to fill
}
struct node nextMove(struct node someNode) {
for(int i = 0; i < 9; i++)
if(someNode.board[i] == ' ') {
someNode.board[i] = 'O';
break;
}
return someNode;
}
int minimax(struct node someNode, int depth, int maximizingPlayer) {
if(depth == 0 || isTerminal(someNode) == TRUE) {
if(someNode.board[0] == someNode.board[3] && someNode.board[3] == someNode.board[6] && someNode.board[6] == 'X')
someNode.value = 1;
if(someNode.board[0] == someNode.board[3] && someNode.board[3] == someNode.board[6] && someNode.board[6] == 'O')
someNode.value = -1;
if(someNode.board[1] == someNode.board[4] && someNode.board[4] == someNode.board[7] && someNode.board[7] == 'X')
someNode.value = 1;//return 1;
if(someNode.board[1] == someNode.board[4] && someNode.board[4] == someNode.board[7] && someNode.board[7] == 'O')
someNode.value = -1;//return -1;
if(someNode.board[2] == someNode.board[5] && someNode.board[5] == someNode.board[8] && someNode.board[8] == 'X')
someNode.value = 1;//return 1;
if(someNode.board[2] == someNode.board[5] && someNode.board[5] == someNode.board[8] && someNode.board[8] == 'O')
someNode.value = -1;//return -1;
if(someNode.board[0] == someNode.board[1] && someNode.board[1] == someNode.board[2] && someNode.board[2] == 'X')
someNode.value = 1;//return 1;
if(someNode.board[0] == someNode.board[1] && someNode.board[1] == someNode.board[2] && someNode.board[2] == 'O')
someNode.value = -1;//return -1;
if(someNode.board[3] == someNode.board[4] && someNode.board[4] == someNode.board[5] && someNode.board[5] == 'X')
someNode.value = 1;//return 1;
if(someNode.board[3] == someNode.board[4] && someNode.board[4] == someNode.board[5] && someNode.board[5] == 'O')
someNode.value = -1;//return -1;
if(someNode.board[6] == someNode.board[7] && someNode.board[7] == someNode.board[8] && someNode.board[8] == 'X')
someNode.value = 1;//return 1;
if(someNode.board[6] == someNode.board[7] && someNode.board[7] == someNode.board[8] && someNode.board[8] == 'O')
someNode.value = -1;//return -1;
if(someNode.board[0] == someNode.board[4] && someNode.board[4] == someNode.board[8] && someNode.board[8] == 'X')
someNode.value = 1;//return 1;
if(someNode.board[0] == someNode.board[4] && someNode.board[4] == someNode.board[8] && someNode.board[8] == 'O')
someNode.value = -1;//return -1;
if(someNode.board[6] == someNode.board[4] && someNode.board[4] == someNode.board[2] && someNode.board[2] == 'X')
someNode.value = 1;//return 1;
if(someNode.board[6] == someNode.board[4] && someNode.board[4] == someNode.board[2] && someNode.board[2] == 'O')
someNode.value = -1;//return -1;
someNode.value = 0;//return 0;
}
if(maximizingPlayer == TRUE) { //maximizing player is cpu i.e. 'O'
someNode.value = NEGINF;
while(isTerminal(someNode) != TRUE)
someNode.value = max(someNode.value, minimax(nextMove(someNode), depth - 1, FALSE));
return someNode.value;
}
else { //minimizing player is me i.e. 'X'
int boxNumber = 0;
scanf("%d", &boxNumber);
someNode.position = boxNumber;
someNode.board[someNode.position] = 'X';
someNode.value = POSINF;
while(isTerminal(someNode) != TRUE)
someNode.value = min(someNode.value, minimax(nextMove(someNode), depth - 1, TRUE));
return someNode.value;
}
}
int main() {
int boxNumber = 0;
printf("Assume you're X and cpu is O \nInput any box number you like \n(0 to 8 both inclusive) \n...you'll be defeated anyways lol :\n");
scanf("%d", &boxNumber);
struct node origin;
for(int i = 0; i < 9; i++)
origin.board[i] = ' ';
origin.position = boxNumber;
origin.board[origin.position] = 'X';
origin.value = 0;
minimax(origin, 8, TRUE);
}
Code version #2 -
#include <stdio.h>
#define FALSE 0
#define TRUE 1
#define NEGINF -10000
#define POSINF +10000
struct node
{
char board[9];
int value;
int position;
};
//char someNode.board[9] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
int max(int value, int returnedValue)
{
return value > returnedValue ? value : returnedValue;
}
int min(int value, int returnedValue)
{
return value < returnedValue ? value : returnedValue;
}
int someoneHasWon(struct node someNode)
{
if (someNode.board[0] == someNode.board[3] && someNode.board[3] == someNode.board[6] && someNode.board[6] == 'x')
return TRUE;
if (someNode.board[0] == someNode.board[3] && someNode.board[3] == someNode.board[6] && someNode.board[6] == 'o')
return TRUE;
//someNode.value = -1;
if (someNode.board[1] == someNode.board[4] && someNode.board[4] == someNode.board[7] && someNode.board[7] == 'x')
return TRUE;
//someNode.value = 1;//return 1;
if (someNode.board[1] == someNode.board[4] && someNode.board[4] == someNode.board[7] && someNode.board[7] == 'o')
return TRUE;
//someNode.value = -1;//return -1;
if (someNode.board[2] == someNode.board[5] && someNode.board[5] == someNode.board[8] && someNode.board[8] == 'x')
return TRUE;
//someNode.value = 1;//return 1;
if (someNode.board[2] == someNode.board[5] && someNode.board[5] == someNode.board[8] && someNode.board[8] == 'o')
return TRUE;
//someNode.value = -1;//return -1;
if (someNode.board[0] == someNode.board[1] && someNode.board[1] == someNode.board[2] && someNode.board[2] == 'x')
return TRUE;
////someNode.value = 1;//return 1;
if (someNode.board[0] == someNode.board[1] && someNode.board[1] == someNode.board[2] && someNode.board[2] == 'o')
return TRUE;
//someNode.value = -1;//return -1;
if (someNode.board[3] == someNode.board[4] && someNode.board[4] == someNode.board[5] && someNode.board[5] == 'x')
return TRUE;
//someNode.value = 1;//return 1;
if (someNode.board[3] == someNode.board[4] && someNode.board[4] == someNode.board[5] && someNode.board[5] == 'o')
return TRUE;
//someNode.value = -1;//return -1;
if (someNode.board[6] == someNode.board[7] && someNode.board[7] == someNode.board[8] && someNode.board[8] == 'x')
return TRUE;
//someNode.value = 1;//return 1;
if (someNode.board[6] == someNode.board[7] && someNode.board[7] == someNode.board[8] && someNode.board[8] == 'o')
return TRUE;
//someNode.value = -1;//return -1;
if (someNode.board[0] == someNode.board[4] && someNode.board[4] == someNode.board[8] && someNode.board[8] == 'x')
return TRUE;
//someNode.value = 1;//return 1;
if (someNode.board[0] == someNode.board[4] && someNode.board[4] == someNode.board[8] && someNode.board[8] == 'o')
return TRUE;
//someNode.value = -1;//return -1;
if (someNode.board[6] == someNode.board[4] && someNode.board[4] == someNode.board[2] && someNode.board[2] == 'x')
return TRUE;
//someNode.value = 1;//return 1;
if (someNode.board[6] == someNode.board[4] && someNode.board[4]
== someNode.board[2] && someNode.board[2] == 'o')
return TRUE;
return FALSE;
}
int isTerminal(struct node someNode)
{
for (int i = 0; i < 9; i++)
if (someNode.board[i] == ' ') // if square left to fill then game is yet incomplete
return FALSE;
if (someoneHasWon(someNode) == TRUE) // if any player has won earlier with squares left
return TRUE;
return TRUE; // if it's a draw with no squares left to fill
}
struct node nextMove(struct node someNode)
{
for (int i = 0; i < 9; i++)
if (someNode.board[i] == ' ')
{
someNode.board[i] = 'o';
break;
}
return someNode;
}
int minimax(struct node someNode, int depth, int maximizingPlayer)
{
if (depth == 8 || isTerminal(someNode) == TRUE)
{
if (someNode.board[0] == someNode.board[3] && someNode.board[3] == someNode.board[6] && someNode.board[6] == 'x')
someNode.value = 1;
if (someNode.board[0] == someNode.board[3] && someNode.board[3] == someNode.board[6] && someNode.board[6] == 'o')
someNode.value = -1;
if (someNode.board[1] == someNode.board[4] && someNode.board[4] == someNode.board[7] && someNode.board[7] == 'x')
someNode.value = 1; //return 1;
if (someNode.board[1] == someNode.board[4] && someNode.board[4] == someNode.board[7] && someNode.board[7] == 'o')
someNode.value = -1; //return -1;
if (someNode.board[2] == someNode.board[5] && someNode.board[5] == someNode.board[8] && someNode.board[8] == 'x')
someNode.value = 1; //return 1;
if (someNode.board[2] == someNode.board[5] && someNode.board[5] == someNode.board[8] && someNode.board[8] == 'o')
someNode.value = -1; //return -1;
if (someNode.board[0] == someNode.board[1] && someNode.board[1] == someNode.board[2] && someNode.board[2] == 'x')
someNode.value = 1; //return 1;
if (someNode.board[0] == someNode.board[1] && someNode.board[1] == someNode.board[2] && someNode.board[2] == 'o')
someNode.value = -1; //return -1;
if (someNode.board[3] == someNode.board[4] && someNode.board[4] == someNode.board[5] && someNode.board[5] == 'x')
someNode.value = 1; //return 1;
if (someNode.board[3] == someNode.board[4] && someNode.board[4] == someNode.board[5] && someNode.board[5] == 'o')
someNode.value = -1; //return -1;
if (someNode.board[6] == someNode.board[7] && someNode.board[7] == someNode.board[8] && someNode.board[8] == 'x')
someNode.value = 1; //return 1;
if (someNode.board[6] == someNode.board[7] && someNode.board[7] == someNode.board[8] && someNode.board[8] == 'o')
someNode.value = -1; //return -1;
if (someNode.board[0] == someNode.board[4] && someNode.board[4] == someNode.board[8] && someNode.board[8] == 'x')
someNode.value = 1; //return 1;
if (someNode.board[0] == someNode.board[4] && someNode.board[4] == someNode.board[8] && someNode.board[8] == 'o')
someNode.value = -1; //return -1;
if (someNode.board[6] == someNode.board[4] && someNode.board[4] == someNode.board[2] && someNode.board[2] == 'x')
someNode.value = 1; //return 1;
if (someNode.board[6] == someNode.board[4] && someNode.board[4] == someNode.board[2] && someNode.board[2] == 'o')
someNode.value = -1; //return -1;
someNode.value = 0; //return 0;
}
if (maximizingPlayer == TRUE)
{ //maximizing player is cpu i.e. 'o'
someNode.value = NEGINF;
while (isTerminal(someNode) != TRUE)
someNode.value = max(someNode.value, minimax(nextMove(someNode), depth + 1, FALSE));
if (someNode.value == -1) {
printf("o %d \n", someNode.value);
return someNode.value;
}
}
else
{ //minimizing player is me i.e. 'x'
int boxNumber = 0;
printf("Enter your move :\n");
scanf("%d", &boxNumber);
someNode.position = boxNumber;
someNode.board[someNode.position] = 'x';
someNode.value = POSINF;
while (isTerminal(someNode) != TRUE)
someNode.value = min(someNode.value, minimax(nextMove(someNode), depth + 1, TRUE));
if (someNode.value == 1)
return someNode.value;
}
}
int main()
{
int boxNumber = 0;
printf("Assume you're x and cpu is O \nInput any box number you like \n(0 to 8 both inclusive) \n...you'll be defeated anyways lol :\n");
scanf("%d", &boxNumber);
struct node origin;
origin.position = boxNumber;
origin.board[origin.position] = 'x';
origin.value = 0;
printf("%c %d \n", origin.board[origin.position], origin.position);
int val = minimax(origin, 1, TRUE);
if(val > 0)
printf("You lose");
else if(val < 0)
printf("You win");
else
printf("It's a draw");
return 0;
}
What I think is that I'm missing a function that actually makes use of the returned 'scores'.
Well, no, the the minimax() function itself is the only one that needs the score it (recursively) computes. The issue is that the pseudocode you're looking at on Wikipedia is very "pseudo". It illustrates the idea of the algorithm reasonably well, but it doesn't provide a very good model for actually implementing it. In particular, it omits a key component that any practical implementation requires: in addition to computing a score, minimax() needs to provide at least one move that leads to that score. That's the move the top-level caller chooses.
Also the nextMove() function confuses me, I want it to check if there is no unfilled box, then fill it with 'O' and then it's the player's turn. But since it will be called again in the recursion, it might not work correctly. Any suggestions?
There are at least two possible general approaches:
make temporary copies of the board as needed, so that you can modify these without changing the original, or
keep track of each position tested, and clear it after the test.
Either way, it's helpful to separate the move analysis process from the actual choice of move.
Note well that you are already making copies by passing struct node objects by value. By itself, that's not necessarily a full or correct implementation of option (1), but it's definitely something to be aware of, especially with respect to my main point, above.
Also, I don't particularly like the pseudocode's approach of using a boolean input to minimax() to indicate whether to maximize or minimize. I prefer instead to pass an argument to indicate which player's move it is. In a symmetric game such as Tic Tac Toe, the minimax() function then makes use of the fact that the better a result is for one player, the worse it is for the other. Among other things, this tends to simplify the code because the code then doesn't need separate cases around the value of that parameter.
Tic-tac-toe is a very simple game, so thus doesn't have too many game-states. So you do not need to limit the AI to a depth.
function bestMove(board, maximisingPlayer) is
if tie then
return {value: 0}
else if maximising player won then
return {value: ∞}
else if minimising player won then
return {value: -∞}
if maximisingPlayer then
value := -∞
move := 0
for each child of node do
tempValue := minimax(child, FALSE).value
if tempValue > value then
value := tempValue
move := child
return {value: value, move: move}
else
value := ∞
move := 0
for each child of node do
tempValue := minimax(child, TRUE).value
if tempValue < value then
value := tempValue
move := child
return {value: value, move: move}
/* Returns best child node after move is made. */
currentBestMove := bestMove(currentBoard, currentPlayer).move
Also as a remark on your code, do not put the win logic in the same procedure as the minimax, try to split your code into smaller functions as much as possible. Also, you may store the win combinations in an array.

Segemention fault (core dumped)

I'm writing some code for school that mimics a simple CPU. I know what segmentation faults are but I can't find what's wrong with my code.
There is nothing wrong with the header or main, I'm including it for referrence
Here is my header:
#define NUM_BYTES (16 * 1024)
#define BYTES_PER_WORD 2
#define WORDS_PER_INSTRUCTION 2
#define NUM_WORDS (NUM_BYTES / BYTES_PER_WORD)
#define NUM_INSTRUCTIONS (NUM_WORDS / WORDS_PER_INSTRUCTION)
#define R0 0
#define R1 1
#define R2 2
#define R3 3
#define R4 4
#define R5 5
typedef unsigned short Machine_word;
typedef enum { PLUS, MINUS, TIMES, DIV, NEG, AND, OR, NOT, LI, LW, SW,
MOVE, CMP, READ, WRITE, HALT } Opcode;
typedef enum { LT, LE, GT, GE, EQ, NE } Comparison;
typedef struct {
Opcode opcode;
Comparison comparison;
unsigned short reg1;
unsigned short reg2;
unsigned short reg3;
unsigned short addr_or_imm;
} Instruction;
void print_instruction(Instruction instr);
int disassemble(const Machine_word memory[], int starting_addr, int num_instrs,
Instruction instrs[], int *const valid_instrs);
int valid_instruction(Machine_word instr_word1, Machine_word instr_word2);
int assemble(unsigned short opcode, unsigned short comparison,
unsigned short reg1, unsigned short reg2, unsigned short reg3,
unsigned short addr_or_imm, Machine_word *const word1,
Machine_word *const word2);
My main:
#include<stdio.h>
#include "header.h"
#define PROGRAM_SIZE 10
int main() {
Machine_word words[NUM_WORDS]= {0x10a5, 0, /* 2 words of 1st instr. */
0x80c0, 0x03ff, /* 2 words of 2nd instr. */
0xa040, 0x00d8, /* etc. */
0x5008, 0,
0x7080, 0,
0xc528, 0x21f8,
0x9080, 0x2718,
0xb058, 0,
0xe100, 0,
0xf000, 0};
/* double braces because it's an array of structures */
Instruction instrs[NUM_INSTRUCTIONS]= {{0}};
int i, num_valid_instrs= 0;
disassemble(words, 0, PROGRAM_SIZE, instrs, &num_valid_instrs);
for (i= 0; i < num_valid_instrs; i++) {
print_instruction(instrs[i]);
printf("\n");
}
My functions: (the first function has no issue!)
#include<stdio.h>
#include "machine.h"
void print_instruction(Instruction instr) {
if (instr.opcode == HALT) {
printf("halt");
} else if (instr.opcode == PLUS) {
printf("plus\tR%hu\tR%hu\tR%hu", instr.reg1, instr.reg2, instr.reg3);
} else if (instr.opcode == MINUS) {
printf("minus\tR%hu\tR%hu\tR%hu", instr.reg1, instr.reg2, instr.reg3);
} else if (instr.opcode == TIMES) {
printf("times\tR%hu\tR%hu\tR%hu", instr.reg1, instr.reg2, instr.reg3);
} else if (instr.opcode == DIV) {
printf("div\tR%hu\tR%hu\tR%hu", instr.reg1, instr.reg2, instr.reg3);
} else if (instr.opcode == NEG) {
printf("neg\tR%hu\tR%hu", instr.reg1, instr.reg2);
} else if (instr.opcode == AND) {
printf("and\tR%hu\tR%hu\tR%hu", instr.reg1, instr.reg2, instr.reg3);
} else if (instr.opcode == OR) {
printf("or\tR%hu\tR%hu\tR%hu", instr.reg1, instr.reg2, instr.reg3);
} else if (instr.opcode == NOT) {
printf("not\tR%hu\tR%hu", instr.reg1, instr.reg2);
} else if(instr.opcode == LI) {
printf("li\tR%hu\t%05hu", instr.reg1, instr.addr_or_imm);
}else if (instr.opcode == LW) {
printf("lw\tR%hu\t%05hu", instr.reg1, instr.addr_or_imm);
} else if (instr.opcode == SW) {
printf("sw\tR%hu\t%05hu", instr.reg1, instr.addr_or_imm);
} else if (instr.opcode == MOVE) {
printf("move\tR%hu\tR%hu", instr.reg1, instr.reg2);
} else if (instr.opcode == CMP) {
printf("cmp %d\tR%hu\tR%hu\t%05hu", (int)instr.comparison, instr.reg1, instr.reg2, instr.addr_or_imm);
} else if (instr.opcode == READ) {
printf("read\tR%hu", instr.reg1);
} else if (instr.opcode == WRITE) {
printf("write\tR%hu", instr.reg1);
}
}
int disassemble(const Machine_word memory[], int starting_addr, int num_instrs, Instruction instrs[], int *const valid_instrs) {
int i, count= 0, index= 0;
if(starting_addr % 4 != 0)
return 0;
if(starting_addr < 0 || starting_addr > 16384)
return 0;
if(starting_addr + num_instrs*4 > 16384)
return 0;
if(memory == NULL || instrs == NULL)
return 0;
for(i = starting_addr/2; i < i + num_instrs*2; i += 2){
/*read instructions*/
Machine_word opcode = memory[i];
Machine_word comparison = memory[i];
Machine_word reg1 = memory[i];
Machine_word reg2 = memory[i];
Machine_word reg3 = memory[i];
Machine_word mem_addr = memory[i + 1];
opcode = (opcode & 0xf000) >> 12;
comparison = (comparison & 0x0e00) >> 9;
reg1 = (reg1 & 0x01c0) >> 6;
reg2 = (reg2 & 0x0038) >> 3;
reg3 = (reg3 & 0x0007);
if(opcode == 0) {
instrs[index].opcode = PLUS;
instrs[index].reg1 = reg1;
instrs[index].reg2 = reg2;
instrs[index].reg3 = reg3;
} else if(opcode == 1) {
instrs[index].opcode = MINUS;
instrs[index].reg1 = reg1;
instrs[index].reg2 = reg2;
instrs[index].reg3 = reg3;
} else if(opcode == 2) {
instrs[index].opcode = TIMES;
instrs[index].reg1 = reg1;
instrs[index].reg2 = reg2;
instrs[index].reg3 = reg3;
} else if(opcode == 3) {
instrs[index].opcode = DIV;
instrs[index].reg1 = reg1;
instrs[index].reg2 = reg2;
instrs[index].reg3 = reg3;
} else if(opcode == 4) {
instrs[index].opcode = NEG;
instrs[index].reg1 = reg1;
instrs[index].reg2 = reg2;
} else if(opcode == 5) {
instrs[index].opcode = AND;
instrs[index].reg1 = reg1;
instrs[index].reg2 = reg2;
instrs[index].reg3 = reg3;
} else if(opcode == 6) {
instrs[index].opcode = OR;
instrs[index].reg1 = reg1;
instrs[index].reg2 = reg2;
instrs[index].reg3 = reg3;
} else if(opcode == 7) {
instrs[index].opcode = NOT;
instrs[index].reg1 = reg1;
instrs[index].reg2 = reg2;
} else if(opcode == 8){
instrs[index].opcode = LI;
instrs[index].reg1 = reg1;
instrs[index].addr_or_imm = mem_addr;
} else if(opcode == 9) {
instrs[index].opcode = LW;
instrs[index].reg1 = reg1;
instrs[index].addr_or_imm = mem_addr;
} else if(opcode == 10) {
instrs[index].opcode = SW;
instrs[index].reg1 = reg1;
instrs[index].addr_or_imm = mem_addr;
} else if(opcode == 11) {
instrs[index].opcode = MOVE;
instrs[index].reg1 = reg1;
instrs[index].reg2 = reg2;
} else if(opcode == 12) {
instrs[index].opcode = CMP;
instrs[index].comparison = (Comparison)comparison;
instrs[index].reg1 = reg1;
instrs[index].reg2 = reg2;
} else if(opcode == 13) {
instrs[index].opcode = READ;
instrs[index].reg1 = reg1;
} else if(opcode == 14) {
instrs[index].opcode = WRITE;
instrs[index].reg1 = reg1;
} else if(opcode == 15) {
instrs[index].opcode = HALT;
}
index++;
count++;
}
*valid_instrs = count;
return 1;
}
Only the second function, which does bit manipulations, has issues
apologies for it being so long.
As told in comments above, the error is in i < i + num_instrs*2 condition. Note, the value of 'i' is not frizzed in i + num_instrs*2 - it is incremented on each step - i. e. condition will looks like 0 < 0 +10*2, 2 < 2 +10*2, 4 < 4 +10*2. So you need to use initial value starting_addr/2 in condition:
Please change
for(i = starting_addr/2; i < i + num_instrs*2; i += 2){
to
for(i = starting_addr/2; i < starting_addr/2 + num_instrs*2; i += 2){

How to infinite loop numbers in while loops? (Unity3D- unityscript)

function OnMouseDown () {
rotationNumber +=1;
}
function Update () {
while (rotationNumber == 1) {
gameObject.GetComponent(SpriteRenderer).sprite = leftArrow;
return;
}
while (rotationNumber == 2) {
gameObject.GetComponent(SpriteRenderer).sprite = upArrow;
return;
}
while (rotationNumber == 3) {
gameObject.GetComponent(SpriteRenderer).sprite = rightArrow;
return;
}
while (rotationNumber == 4) {
gameObject.GetComponent(SpriteRenderer).sprite = upArrow;
rotationNumber = 1;
return;
}
}
I want to loop this but when I click it the fourth time it just goes straight to the first image. I tried yield WaitForSeconds but it didn't work.
The problem is that you are setting rotationNumber to 1 inside the while loop. Do it in this way:
function OnMouseDown () {
rotationNumber += 1;
if ( rotationNumber > 4 ) rotationNumber = 1;
}
function Update () {
while (rotationNumber == 1) {
gameObject.GetComponent(SpriteRenderer).sprite = leftArrow;
return;
}
while (rotationNumber == 2) {
gameObject.GetComponent(SpriteRenderer).sprite = upArrow;
return;
}
while (rotationNumber == 3) {
gameObject.GetComponent(SpriteRenderer).sprite = rightArrow;
return;
}
while (rotationNumber == 4) {
gameObject.GetComponent(SpriteRenderer).sprite = upArrow;
return;
}
}
I'm not quite sure why you are using a while loop within in an update loop, but try this below. Every time you click the rotation number increases and the sprite is changed until the rotationNumber variable is > 4 in which case it resets the variable back to 1.
function Update()
{
if(rotationNumber == 1)
{
gameObject.GetComponent(SpriteRenderer).sprite = leftArrow;
}else if(rotationNumber ==2)
{
gameObject.GetComponent(SpriteRenderer).sprite = upArrow;
}else if(rotationNumber ==3)
{
gameObject.GetComponent(SpriteRenderer).sprite = rightArrow;
}else if(rotationNumber == 4)
{
gameObject.GetComponent(SpriteRenderer).sprite = upArrow;
}else if(rotationNumber > 4)
{
rotationNumber = 1;
}
}

Errors in my Tic-Tac-Toe Game

Here's my code for my tic-tac-toe game:
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
int board[3][3] = {
{0, 0, 0},
{0, 0, 0},
{0, 0, 0}
};
int main (void)
{
int const user1 = 1;
int const user2 = 2;
char move[10];
while (! all_locations_filled()) {
printf("User-1, please enter your move:");
scanf("%s", move);
if(valid_location(move)) {
mark_location(user1, move);
display_board(board[3][3]);
}
else if(won_the_game(user1)) {
printf("Congratulations User-1, You Won the Game!");
break;
}
else {
printf("Invalid Move");
}
printf("User-2, please enter your move:");
scanf("%s", move);
if(valid_location(move)) {
mark_location(user2, move);
display_board();
}
else if(won_the_game(user2) {
printf("Congratulations User-2, You Won the Game!");
break;
}
else {
printf("Invalid Move");
}
}
return 0;
}
bool valid_location(char str[10]) {
int strcmp(x, y);
if (strcmp(str[10], "upperLeft") == 0 || strcmp(str[10], "up") == 0 || strcmp(str[10], "upperRight") == 0 || strcmp(str[10], "left") == 0 || strcmp(str[10], "center") == 0 || strcmp(str[10], "right") == 0 || strcmp(str[10], "lowerLeft") == 0 || strcmp(str[10], "down") == 0 || strcmp(str[10], "lowerRight") == 0)
return true;
}
void mark_location(int userU, char str[10]) {
int strcmp(x, y);
if (strcmp(str[10], "upperLeft") == 0)
board[0][0] = userU;
else if (strcmp(str[10], "up") == 0)
board[0][1] = userU;
else if (strcmp(str[10], "upperRight") == 0)
board[0][2] = userU;
else if (strcmp(str[10], "left") == 0)
board[1][0] = userU;
else if (strcmp(str[10], "center") == 0)
board[1][1] = userU;
else if (strcmp(str[10], "right") == 0)
board[1][2] = userU;
else if (strcmp(str[10], "lowerLeft") == 0)
board[2][0] = userU;
else if (strcmp(str[10], "down") == 0)
board[2][1] = userU;
else if (strcmp(str[10], "lowerRight") == 0)
board [2][2] = userU;
}
char display_board(int array[][]) {
int i, j;
for (i=0; i<3; ++i)
for (j=0; j<3; ++j)
if (array[i][j] == 0)
print("-");
else if (array[i][j] == 1)
print("x");
else if (array[i][j] == 2)
print("o");
}
bool all_locations_filled() {
int i, j;
for (i=0; i<3; ++i)
for (j=0; j<3; ++j)
if board[i][j] == 0
return false;
return true;
}
bool won_the_game(userU) {
int i, j;
if (board[0][0] == userU && board[0][1] == userU && board[0][2] == userU)
return true;
else if (board[1][0] == userU && board[1][1] == userU && board[1][2] == userU)
return true;
else if (board[2][0] == userU && board[2][1] == userU && board[2][2] == userU)
return true;
else if (board[0][0] == userU && board[1][0] == userU && board[2][0] == userU)
return true;
else if (board[0][1] == userU && board[1][1] == userU && board[2][1] == userU)
return true;
else if (board[0][2] == userU && board[1][2] == userU && board[2][2] == userU)
return true;
else if (board[0][0] == userU && board[1][1] == userU && board[2][2] == userU)
return true;
else if (board[2][2] == userU && board[1][1] == userU && board[2][0] == userU)
return true;
else
return false;
}
There are a few errors that I don't understand, here they are:
tictactoe.c:50: error: expected expression before ‘}’ token
This error is at the end of the main function but I'm not sure what I did wrong.
tictactoe.c:52: error: nested functions are disabled, use -fnested-functions to re-enable
I didn't know I used a nested function.
tictactoe.c:53: warning: parameter names (without types) in function declaration
This is referring to int strcmp(x, y)
tictactoe.c:55: warning: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast
What did I do wrong with strcmp?
If someone could help me out I'd greatly appreciate it.
You're missing a closing parenthesis here (line #40):
else if(won_the_game(user2) {
Should be:
else if(won_the_game(user2)) {
You have a couple or problems with the strcmp as well.
strcmp(str[10], "upperRight")
The compiler is complaining about the first parameter str[10]. One problem is that this selects a single character from the string, and not the whole string. Another problem is that in an array of size 10, the positions are numbered 0..9 so there isn't even a position 10!
Also, a string literal like "upperRight" contains 10 visible characters plus an extra zero character as a terminator. So it needs 11 positions when stored in the str.

Resources