/* Tic Tac Toe  */

// EE 1520 - Embedded Systems I
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define XPLAYER  0
#define OPLAYER  1
#define OPEN    -77

#define TIEGAME  2
#define LIVEGAME 3

int electrician[9];

// Prototypes of subroutines involved
void initialize_board();
int check_winner();
int check_valid_move();
void display_board();

//  Tic-Tac-Toe
//
//     |   |
//  ------------
//     |   |
//  ------------
//     |   |
//
// The array "electrician" is tracking the following:
//   0 | 1 | 2
//  ------------
//   3 | 4 | 5
//  ------------
//   6 | 7 | 8
//
// The array "electrician" is tracking the following:


void initialize_board()
{
  // STUDENT : write code to initialize all board spots to OPEN
int index;
for (index = 0; index < 9; index++)
electrician[index] = OPEN;

}

int check_winner()
{
  // STUDENT : write code to see if player has won game
  // Return 0 if game is won by XPLAYER
  // Return 1 if game is won by OPLAYER
  // Return 2 if game is a tie
  // Return 3 if game is still going on
int index;


if (electrician[0] == XPLAYER && electrician[1] == XPLAYER && electrician[2] == XPLAYER){
return 0;}
if (electrician[3] == XPLAYER && electrician[4] == XPLAYER && electrician[5] == XPLAYER){
return 0;}
if (electrician[6] == XPLAYER && electrician[7] == XPLAYER && electrician[8] == XPLAYER){
return 0;}
if (electrician[0] == XPLAYER && electrician[4] == XPLAYER && electrician[8] == XPLAYER){
return 0;}
if (electrician[2] == XPLAYER && electrician[4] == XPLAYER && electrician[6] == XPLAYER){
return 0;}
if (electrician[0] == XPLAYER && electrician[3] == XPLAYER && electrician[6] == XPLAYER){
return 0;}
if (electrician[1] == XPLAYER && electrician[4] == XPLAYER && electrician[7] == XPLAYER){
return 0;}
if (electrician[2] == XPLAYER && electrician[5] == XPLAYER && electrician[8] == XPLAYER){
return 0;}
if (electrician[0] == OPLAYER && electrician[1] == OPLAYER && electrician[2] == OPLAYER){
return 1;}
if (electrician[3] == OPLAYER && electrician[4] == OPLAYER && electrician[5] == OPLAYER){
return 1;}
if (electrician[6] == OPLAYER && electrician[7] == OPLAYER && electrician[8] == OPLAYER){
return 1;}
if (electrician[0] == OPLAYER && electrician[4] == OPLAYER && electrician[8] == OPLAYER){
return 1;}
if (electrician[2] == OPLAYER && electrician[4] == OPLAYER && electrician[6] == OPLAYER){
return 1;}
if (electrician[0] == OPLAYER && electrician[3] == OPLAYER && electrician[6] == OPLAYER){
return 1;}
if (electrician[1] == OPLAYER && electrician[4] == OPLAYER && electrician[7] == OPLAYER){
return 1;}
if (electrician[2] == OPLAYER && electrician[5] == OPLAYER && electrician[8] == OPLAYER){
return 1;}
for (index = 0; index < 9; index++)
if (electrician[index] == OPEN)
return 3;
if (electrician[0] >= 0 && electrician[1] >=0 && electrician[2] >= 0 && electrician[3] >= 0 && electrician[4] >= 0 && electrician[5] >= 0 && electrician[6] >= 0 && electrician[7] >= 0 && electrician[8] >= 0){
return 2;}


}

int check_valid_move(int spot)
{
  // STUDENT : write code to see if player has won game
  // Return 0 if spot is not valid
  // Return 1 if spot is valid
if (spot >= 0 && spot < 9 && electrician[spot] == OPEN)
return 1;
else
return 0;

}

char GP(int board_item)
{
  if (board_item == XPLAYER) {
   return 'X';
  }
  else if (board_item == OPLAYER) {
   return 'O';
  }
  else {
   return ' ';
  }
}

void display_board()
{
  printf("\n");
  printf("//   0 | 1 | 2 \n");
  printf("//  ------------ \n");
  printf("//   3 | 4 | 5 \n");
  printf("//  ------------ \n");
  printf("//   6 | 7 | 8 \n");
  printf("\n\n");
  printf("//   %c | %c | %c  \n", GP(electrician[0]),
GP(electrician[1]), GP(electrician[2]));
  printf("//  ------------\n");
  printf("//   %c | %c | %c  \n", GP(electrician[3]),
GP(electrician[4]), GP(electrician[5]));
  printf("//  ------------\n");
  printf("//   %c | %c | %c  \n", GP(electrician[6]),
GP(electrician[7]), GP(electrician[8]));
  printf("\n");
}

int main(int argc, char **argv)
{
  int i;
  int turn = XPLAYER;
  int move;
  int winner;

  // Initalize the board
  initialize_board();

  // Display the board
  display_board();

  while (check_winner() == LIVEGAME) {

     if (turn==XPLAYER) {
         printf("Player X - Enter a position [0-8]?\n");
     }
     else {
         printf("Player O - Enter a position [0-8]?\n");
     }
     scanf("%d", &move);

     // Check to see if the move was valid
     if (!check_valid_move(move))
continue;

     // Update the move on the board
     gameboard[move] = turn;

     // Show the current board
     display_board();

     // Switch to the other player
     turn = 1 - turn;
  }

  // Print a message about the game end
  winner = check_winner();
  if (winner == XPLAYER) {
   printf("Player X is the winner\n");
  }
  else if (winner == OPLAYER) {
   printf("Player O is the winner\n");
  }
  else {
   printf("Game is a tie\n");
  }


  return 0;
}

// Fibonacci number sequence
#include <stdio.h>

main( ) {
   int a = 0;
   int b = 1;
   int    i;
   int c = 0;
   int fib_numbers[40];
  
   for (i = 0; i <= 40; i++){
       c = a + b;
       fib_numbers[i] = c;
       printf("%d\n", fib_numbers[40]);
       a = b;
       b = c; }
   return 0;
}

// Fraction in lowest terms

#include <stdio.h>
/*    fraction in lowest terms   */

main()
{
   int i = 2, num, denom;
   printf("enter a fraction: ");
   scanf("%d/%d", &num, &denom);
   if (denom == 0){
       printf("cannot divide by 0");}
   if (num == 0) {
       printf("0");}
   for (i = 2; i <= num && i <= denom; i++) {
       while (num%i == 0 && denom%i == 0) {
       num /= i;
       denom /= i;
       }
   }
   printf("fraction in lowest terms: %d/%d", num, denom);
   return 0;
}



// Two largest numbers in an array
// This is only a function

void find_two_largest (int a[], int size, int *largest, int *second_largest)
{
int x, y;
   int i = 0;
x = 0;
y = 0;

for (i = 0; i < size; i++){
if (a[i] >= x){
y = x;
x = a[i];
}
else if (a[i] >= y){
y = a[i];

}
}
*largest = x;
*second_largest = y;


}
// Hello world
#include <stdio.h>
#include <stdlib.h>

void main ()
{
printf("hello, world");
}
C Programming Examples