SlideShare a Scribd company logo
1 of 12
Download to read offline
#include <stdio.h>
#include <stdlib.h> // malloc
#include <time.h> // srand
#include <unistd.h> // optarg
char **create_board(int board_sz);
void print_board(char **Board, int board_sz);
int is_draw(char **Board, int board_sz);
char winning_move(char **Board, int board_sz, int row_index, int col_index);
// Prints the Board
void print_board(char **Board, int board_sz)
{
printf(" ");
for (int i = 0; i < board_sz; i++)
{
printf("|%d", i);
}
printf("|n");
for (int i = 0; i < board_sz; i++)
{
printf("%c", 'a' + i);
for (int j = 0; j < board_sz; j++)
{
printf("|%c", Board[i][j]);
}
printf("|n");
}
}
// Creates nxn tic tac toe Board
char **create_board(int board_sz)
{
char **Board = (char **)malloc(board_sz * sizeof(char *));
for (int i = 0; i < board_sz; i++)
{
Board[i] = (char *)malloc(board_sz * sizeof(char));
}
for (int i = 0; i < board_sz; i++)
{
for (int j = 0; j < board_sz; j++)
{
Board[i][j] = ' ';
}
}
return Board;
}
// Returns true if the game is a draw
int is_draw(char **Board, int board_sz)
{
for (int i = 0; i < board_sz; i++)
{
for (int j = 0; j < board_sz; j++)
{
if (Board[i][j] == ' ')
{
// empty square, so game ain't over yet
return 0;
}
}
}
// no empty squares, so it's a draw
return 1;
}
// Returns 'X' if (i,j) was a winning move for X
// Returns 'O' if (i,j) was a winning move for O
// Returns ASCII value 0 otherwise
char winning_move(char **Board, int board_sz, int row_index, int col_index)
{
int win = 1;
// check row
for (int k = 0; k < board_sz; k++)
{
if (Board[row_index][k] != Board[row_index][col_index])
{
win = 0;
break;
}
}
if (win) // means Board[i][k] == Board[i][j]
{
return Board[row_index][col_index];
}
// check column
win = 1;
for (int k = 0; k < board_sz; k++)
{
if (Board[k][col_index] != Board[row_index][col_index])
{
win = 0;
break;
}
}
if (win)
{
return Board[row_index][col_index];
}
// check forward diagonal
win = 1;
for (int k = 0; k < board_sz; k++)
{
if (Board[k][k] != Board[row_index][col_index])
{
win = 0;
break;
}
}
if (win)
{
return Board[row_index][col_index];
}
// check reverse diagonal
win = 1;
for (int k = 0; k < board_sz; k++)
{
if (Board[k][board_sz - k - 1] != Board[row_index][col_index])
{
win = 0;
break;
}
}
if (win)
{
return Board[row_index][col_index];
}
// got nothing
return 0;
}
// -------------------------getMove -------------------------------
int getMove( char ** Board, int board_sz, char *x, char *y ){
Board = create_board(board_sz);
char winner = '0';
char row = *x;
char col = *y;
char turn = 'X';
char ch;
int computer_enabled = 0;
if (turn == 'X' || !computer_enabled)
{
printf("computer 'O' Moves are 'enabled'n");
printf("-Player's %c turn (qq to quit)nn", turn);
// suggestion
do
{
row = rand() % board_sz + 'a'; //
col = rand() % board_sz + '0';
} while (Board[row - 'a'][col - '1'] != ' ');
printf("*---> suggestion:t(%c %c)nn", row, col);
printf("(X) Enter Move (row column) ---------------------------> ");
fflush(stdout);
scanf(" %c %c", &row, &col);
// quit when enter qq.
if (row == 'q' && col == 'q'){
exit(0);
}
}
else
{
printf("computer 'O' Moves are 'enabled'n");
printf("*Player's O turn (qq to quit)nnn");
// Randomly pick a move
do
{
row = rand() % board_sz + 'a'; //
col = rand() % board_sz + '0';
} while (Board[row - 'a'][col - '1'] != ' ');
printf("(O) computer Picks (%c %c) (hit a key to continue) ----> ", row, col);
}
// Make move if square is free
int rowind = row - 'a';
int colind = col - '0';
if (rowind >= board_sz || colind >= board_sz)
{
printf("Invalid moven");
}
else if (Board[rowind][colind] == ' ')
{
char enter;
enter = getchar();
printf("Move is %c %c (%d, %d)n", row, col, rowind, colind);
Board[rowind][colind] = turn;
if (turn == 'X')
{
turn = 'O';
}
else
{
turn = 'X';
}
winner = winning_move(Board, board_sz, rowind, colind);
}
else
{
printf("Square is occupied; try again.n");
}
// Game over - print Board & declare finish
print_board(Board, board_sz);
if (winner == 'X' || winner == 'O')
{
printf("Congratulations %c!n", winner);
}
else
{
printf("Game ends in a draw.n");
}
// Free memory
for (int i = 0; i < board_sz; i++)
{
free(Board[i]);
}
free(Board);
return 0;
}
// -----------------------------------------------------------------------
// main function where program start
int main(int argc, char **argv)
{
int opt;
int board_sz = 3;
int computer_enabled = 0;
srand(time(NULL));
// Parse command line arguments using getopt()
while ((opt = getopt(argc, argv, "s:i")) != -1)
{
switch (opt)
{
case 's':
board_sz = atoi(optarg);
break;
case 'i':
computer_enabled = 1;
break;
default:
break;
}
}
char **Board = create_board(board_sz);
char winner = '0';
char *row;
char *col;
char turn = 'X';
char ch;
// standard game loop
while (!winner && !is_draw(Board, board_sz))
{
print_board(Board, board_sz);
getMove(Board, board_sz, *row, *col);
return 0;
}
}
this is tic-tac-toe game code in c.
but i got a error here.
could you please fix this code and explain why the error occurs here?
please fix the code and show the run result also. thank you.
C split_copy.c 2 incompatible integer to pointer conversion passing 'char' to parameter of type
'char *'; remove * [-Wint-conversion] gcc [Ln 273, Col 34] incompatible integer to pointer
conversion passing 'char' to parameter of type 'char ; remove [ Wint-conversion] gcc [Ln 273,
Col 40]
#include -stdio-h- #include -stdlib-h- -- malloc #include -time-h- --.pdf

More Related Content

Similar to #include -stdio-h- #include -stdlib-h- -- malloc #include -time-h- --.pdf

#include stdio.h #include string.h #include stdlib.h #in.pdf
#include stdio.h #include string.h #include stdlib.h #in.pdf#include stdio.h #include string.h #include stdlib.h #in.pdf
#include stdio.h #include string.h #include stdlib.h #in.pdf
singhanubhav1234
 
include ltstdiohgt include ltstdlibhgt include .pdf
include ltstdiohgt include ltstdlibhgt include .pdfinclude ltstdiohgt include ltstdlibhgt include .pdf
include ltstdiohgt include ltstdlibhgt include .pdf
adisainternational
 
i have a runable code below that works with just guessing where the .pdf
i have a runable code below that works with just guessing where the .pdfi have a runable code below that works with just guessing where the .pdf
i have a runable code below that works with just guessing where the .pdf
armcomputers
 
i need an input of this program.  anything good or bad.  what could .docx
i need an input of this program.  anything good or bad.  what could .docxi need an input of this program.  anything good or bad.  what could .docx
i need an input of this program.  anything good or bad.  what could .docx
ursabrooks36447
 
include ltstdiohgtinclude ltstdlibhgtinclude l.pdf
include ltstdiohgtinclude ltstdlibhgtinclude l.pdfinclude ltstdiohgtinclude ltstdlibhgtinclude l.pdf
include ltstdiohgtinclude ltstdlibhgtinclude l.pdf
adisainternational
 
This is the Java code i have for a Battleship project i am working o.pdf
This is the Java code i have for a Battleship project i am working o.pdfThis is the Java code i have for a Battleship project i am working o.pdf
This is the Java code i have for a Battleship project i am working o.pdf
calderoncasto9163
 
AI CHALLENGE ADMIN
AI CHALLENGE ADMINAI CHALLENGE ADMIN
AI CHALLENGE ADMIN
Ankit Gupta
 
GameOfLife.cs using System; using System.Collections.Generic;.pdf
GameOfLife.cs using System; using System.Collections.Generic;.pdfGameOfLife.cs using System; using System.Collections.Generic;.pdf
GameOfLife.cs using System; using System.Collections.Generic;.pdf
aravlitraders2012
 
i have a code that runs, but it only lets the player to guess where .pdf
i have a code that runs, but it only lets the player to guess where .pdfi have a code that runs, but it only lets the player to guess where .pdf
i have a code that runs, but it only lets the player to guess where .pdf
poblettesedanoree498
 
The main class of the tictoe game looks like.public class Main {.pdf
The main class of the tictoe game looks like.public class Main {.pdfThe main class of the tictoe game looks like.public class Main {.pdf
The main class of the tictoe game looks like.public class Main {.pdf
asif1401
 
The following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdfThe following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdf
fonecomp
 
Here is the code for youimport java.util.Scanner; import java.u.pdf
Here is the code for youimport java.util.Scanner; import java.u.pdfHere is the code for youimport java.util.Scanner; import java.u.pdf
Here is the code for youimport java.util.Scanner; import java.u.pdf
anithareadymade
 

Similar to #include -stdio-h- #include -stdlib-h- -- malloc #include -time-h- --.pdf (15)

#include stdio.h #include string.h #include stdlib.h #in.pdf
#include stdio.h #include string.h #include stdlib.h #in.pdf#include stdio.h #include string.h #include stdlib.h #in.pdf
#include stdio.h #include string.h #include stdlib.h #in.pdf
 
include ltstdiohgt include ltstdlibhgt include .pdf
include ltstdiohgt include ltstdlibhgt include .pdfinclude ltstdiohgt include ltstdlibhgt include .pdf
include ltstdiohgt include ltstdlibhgt include .pdf
 
i have a runable code below that works with just guessing where the .pdf
i have a runable code below that works with just guessing where the .pdfi have a runable code below that works with just guessing where the .pdf
i have a runable code below that works with just guessing where the .pdf
 
i need an input of this program.  anything good or bad.  what could .docx
i need an input of this program.  anything good or bad.  what could .docxi need an input of this program.  anything good or bad.  what could .docx
i need an input of this program.  anything good or bad.  what could .docx
 
include ltstdiohgtinclude ltstdlibhgtinclude l.pdf
include ltstdiohgtinclude ltstdlibhgtinclude l.pdfinclude ltstdiohgtinclude ltstdlibhgtinclude l.pdf
include ltstdiohgtinclude ltstdlibhgtinclude l.pdf
 
This is the Java code i have for a Battleship project i am working o.pdf
This is the Java code i have for a Battleship project i am working o.pdfThis is the Java code i have for a Battleship project i am working o.pdf
This is the Java code i have for a Battleship project i am working o.pdf
 
VTU Data Structures Lab Manual
VTU Data Structures Lab ManualVTU Data Structures Lab Manual
VTU Data Structures Lab Manual
 
AI CHALLENGE ADMIN
AI CHALLENGE ADMINAI CHALLENGE ADMIN
AI CHALLENGE ADMIN
 
GameOfLife.cs using System; using System.Collections.Generic;.pdf
GameOfLife.cs using System; using System.Collections.Generic;.pdfGameOfLife.cs using System; using System.Collections.Generic;.pdf
GameOfLife.cs using System; using System.Collections.Generic;.pdf
 
i have a code that runs, but it only lets the player to guess where .pdf
i have a code that runs, but it only lets the player to guess where .pdfi have a code that runs, but it only lets the player to guess where .pdf
i have a code that runs, but it only lets the player to guess where .pdf
 
The main class of the tictoe game looks like.public class Main {.pdf
The main class of the tictoe game looks like.public class Main {.pdfThe main class of the tictoe game looks like.public class Main {.pdf
The main class of the tictoe game looks like.public class Main {.pdf
 
Connect 4
Connect 4Connect 4
Connect 4
 
The following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdfThe following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdf
 
Here is the code for youimport java.util.Scanner; import java.u.pdf
Here is the code for youimport java.util.Scanner; import java.u.pdfHere is the code for youimport java.util.Scanner; import java.u.pdf
Here is the code for youimport java.util.Scanner; import java.u.pdf
 
Arduino coding class
Arduino coding classArduino coding class
Arduino coding class
 

More from JacobQfDNolanr

--php include 'view-header-php'- -- -main- -nav- -h2-Administ.pdf
--php include 'view-header-php'- -- -main-     -nav-      -h2-Administ.pdf--php include 'view-header-php'- -- -main-     -nav-      -h2-Administ.pdf
--php include 'view-header-php'- -- -main- -nav- -h2-Administ.pdf
JacobQfDNolanr
 
-- index-php --php include 'view-header-php'- -- -main- -nav-.pdf
-- index-php --php include 'view-header-php'- -- -main-     -nav-.pdf-- index-php --php include 'view-header-php'- -- -main-     -nav-.pdf
-- index-php --php include 'view-header-php'- -- -main- -nav-.pdf
JacobQfDNolanr
 

More from JacobQfDNolanr (20)

(2) Find the exact solution of the following recurrence- Show your ans.pdf
(2) Find the exact solution of the following recurrence- Show your ans.pdf(2) Find the exact solution of the following recurrence- Show your ans.pdf
(2) Find the exact solution of the following recurrence- Show your ans.pdf
 
(a) How many cubtomens were survered- (0) Wiy are theie byariate data-.pdf
(a) How many cubtomens were survered- (0) Wiy are theie byariate data-.pdf(a) How many cubtomens were survered- (0) Wiy are theie byariate data-.pdf
(a) How many cubtomens were survered- (0) Wiy are theie byariate data-.pdf
 
(a) Draw the component architecture of database system environment- Il.pdf
(a) Draw the component architecture of database system environment- Il.pdf(a) Draw the component architecture of database system environment- Il.pdf
(a) Draw the component architecture of database system environment- Il.pdf
 
(3-) How is sulfur reduction accomplished by some bacteria- (4-) How i.pdf
(3-) How is sulfur reduction accomplished by some bacteria- (4-) How i.pdf(3-) How is sulfur reduction accomplished by some bacteria- (4-) How i.pdf
(3-) How is sulfur reduction accomplished by some bacteria- (4-) How i.pdf
 
(a) Determine the probabilty that boch contain dist soda Plooth diet)-.pdf
(a) Determine the probabilty that boch contain dist soda Plooth diet)-.pdf(a) Determine the probabilty that boch contain dist soda Plooth diet)-.pdf
(a) Determine the probabilty that boch contain dist soda Plooth diet)-.pdf
 
(1 point) The Census Bureau reports that 82- of Americans over the age.pdf
(1 point) The Census Bureau reports that 82- of Americans over the age.pdf(1 point) The Census Bureau reports that 82- of Americans over the age.pdf
(1 point) The Census Bureau reports that 82- of Americans over the age.pdf
 
(1-) Define the term 'positive control- what are some examples of 'fal.pdf
(1-) Define the term 'positive control- what are some examples of 'fal.pdf(1-) Define the term 'positive control- what are some examples of 'fal.pdf
(1-) Define the term 'positive control- what are some examples of 'fal.pdf
 
(20 points) Little weeping and wailing was heard when the reclusive wi.pdf
(20 points) Little weeping and wailing was heard when the reclusive wi.pdf(20 points) Little weeping and wailing was heard when the reclusive wi.pdf
(20 points) Little weeping and wailing was heard when the reclusive wi.pdf
 
-What is the purpose of Class Design- - In what ways are classes refin.pdf
-What is the purpose of Class Design- - In what ways are classes refin.pdf-What is the purpose of Class Design- - In what ways are classes refin.pdf
-What is the purpose of Class Design- - In what ways are classes refin.pdf
 
-Select- Question 3 all of these choices are correct- Water dissolves.pdf
-Select- Question 3 all of these choices are correct- Water dissolves.pdf-Select- Question 3 all of these choices are correct- Water dissolves.pdf
-Select- Question 3 all of these choices are correct- Water dissolves.pdf
 
-One word answer- The original motivational theory - -Theory X and Y i.pdf
-One word answer- The original motivational theory - -Theory X and Y i.pdf-One word answer- The original motivational theory - -Theory X and Y i.pdf
-One word answer- The original motivational theory - -Theory X and Y i.pdf
 
-green dot Unanswered Open -green dot Unanswered Identify the structur.pdf
-green dot Unanswered Open -green dot Unanswered Identify the structur.pdf-green dot Unanswered Open -green dot Unanswered Identify the structur.pdf
-green dot Unanswered Open -green dot Unanswered Identify the structur.pdf
 
-One of the deepest unresolved paradoxes in evolutionary biology is th.pdf
-One of the deepest unresolved paradoxes in evolutionary biology is th.pdf-One of the deepest unresolved paradoxes in evolutionary biology is th.pdf
-One of the deepest unresolved paradoxes in evolutionary biology is th.pdf
 
-15 pts 7-5 pts each- A Starburst candy package contains 12 individual.pdf
-15 pts 7-5 pts each- A Starburst candy package contains 12 individual.pdf-15 pts 7-5 pts each- A Starburst candy package contains 12 individual.pdf
-15 pts 7-5 pts each- A Starburst candy package contains 12 individual.pdf
 
-12-13 (Count characters- words- and lines in a file) Write a program.pdf
-12-13 (Count characters- words- and lines in a file) Write a program.pdf-12-13 (Count characters- words- and lines in a file) Write a program.pdf
-12-13 (Count characters- words- and lines in a file) Write a program.pdf
 
--NO COPY PAST PLEASE-- Critical National Information Infrastructure.pdf
--NO COPY PAST PLEASE--  Critical National Information Infrastructure.pdf--NO COPY PAST PLEASE--  Critical National Information Infrastructure.pdf
--NO COPY PAST PLEASE-- Critical National Information Infrastructure.pdf
 
--php include 'view-header-php'- -- -main- -nav- -h2-Administ.pdf
--php include 'view-header-php'- -- -main-     -nav-      -h2-Administ.pdf--php include 'view-header-php'- -- -main-     -nav-      -h2-Administ.pdf
--php include 'view-header-php'- -- -main- -nav- -h2-Administ.pdf
 
-- index-php --php include 'view-header-php'- -- -main- -nav-.pdf
-- index-php --php include 'view-header-php'- -- -main-     -nav-.pdf-- index-php --php include 'view-header-php'- -- -main-     -nav-.pdf
-- index-php --php include 'view-header-php'- -- -main- -nav-.pdf
 
- Exercise Seven 3 usages I github-dassroom-bot- public static String-.pdf
- Exercise Seven 3 usages I github-dassroom-bot- public static String-.pdf- Exercise Seven 3 usages I github-dassroom-bot- public static String-.pdf
- Exercise Seven 3 usages I github-dassroom-bot- public static String-.pdf
 
- A scientist is studying stem length in wheat plants- She crosses two.pdf
- A scientist is studying stem length in wheat plants- She crosses two.pdf- A scientist is studying stem length in wheat plants- She crosses two.pdf
- A scientist is studying stem length in wheat plants- She crosses two.pdf
 

Recently uploaded

MSc Ag Genetics & Plant Breeding: Insights from Previous Year JNKVV Entrance ...
MSc Ag Genetics & Plant Breeding: Insights from Previous Year JNKVV Entrance ...MSc Ag Genetics & Plant Breeding: Insights from Previous Year JNKVV Entrance ...
MSc Ag Genetics & Plant Breeding: Insights from Previous Year JNKVV Entrance ...
Krashi Coaching
 

Recently uploaded (20)

II BIOSENSOR PRINCIPLE APPLICATIONS AND WORKING II
II BIOSENSOR PRINCIPLE APPLICATIONS AND WORKING IIII BIOSENSOR PRINCIPLE APPLICATIONS AND WORKING II
II BIOSENSOR PRINCIPLE APPLICATIONS AND WORKING II
 
Book Review of Run For Your Life Powerpoint
Book Review of Run For Your Life PowerpointBook Review of Run For Your Life Powerpoint
Book Review of Run For Your Life Powerpoint
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
MSc Ag Genetics & Plant Breeding: Insights from Previous Year JNKVV Entrance ...
MSc Ag Genetics & Plant Breeding: Insights from Previous Year JNKVV Entrance ...MSc Ag Genetics & Plant Breeding: Insights from Previous Year JNKVV Entrance ...
MSc Ag Genetics & Plant Breeding: Insights from Previous Year JNKVV Entrance ...
 
philosophy and it's principles based on the life
philosophy and it's principles based on the lifephilosophy and it's principles based on the life
philosophy and it's principles based on the life
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio App
 
“O BEIJO” EM ARTE .
“O BEIJO” EM ARTE                       .“O BEIJO” EM ARTE                       .
“O BEIJO” EM ARTE .
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
How to Manage Closest Location in Odoo 17 Inventory
How to Manage Closest Location in Odoo 17 InventoryHow to Manage Closest Location in Odoo 17 Inventory
How to Manage Closest Location in Odoo 17 Inventory
 
Features of Video Calls in the Discuss Module in Odoo 17
Features of Video Calls in the Discuss Module in Odoo 17Features of Video Calls in the Discuss Module in Odoo 17
Features of Video Calls in the Discuss Module in Odoo 17
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 
How to Analyse Profit of a Sales Order in Odoo 17
How to Analyse Profit of a Sales Order in Odoo 17How to Analyse Profit of a Sales Order in Odoo 17
How to Analyse Profit of a Sales Order in Odoo 17
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
 
IPL Online Quiz by Pragya; Question Set.
IPL Online Quiz by Pragya; Question Set.IPL Online Quiz by Pragya; Question Set.
IPL Online Quiz by Pragya; Question Set.
 
Implanted Devices - VP Shunts: EMGuidewire's Radiology Reading Room
Implanted Devices - VP Shunts: EMGuidewire's Radiology Reading RoomImplanted Devices - VP Shunts: EMGuidewire's Radiology Reading Room
Implanted Devices - VP Shunts: EMGuidewire's Radiology Reading Room
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
demyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptxdemyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptx
 

#include -stdio-h- #include -stdlib-h- -- malloc #include -time-h- --.pdf

  • 1. #include <stdio.h> #include <stdlib.h> // malloc #include <time.h> // srand #include <unistd.h> // optarg char **create_board(int board_sz); void print_board(char **Board, int board_sz); int is_draw(char **Board, int board_sz); char winning_move(char **Board, int board_sz, int row_index, int col_index); // Prints the Board void print_board(char **Board, int board_sz) { printf(" "); for (int i = 0; i < board_sz; i++) { printf("|%d", i); } printf("|n"); for (int i = 0; i < board_sz; i++) { printf("%c", 'a' + i); for (int j = 0; j < board_sz; j++) { printf("|%c", Board[i][j]);
  • 2. } printf("|n"); } } // Creates nxn tic tac toe Board char **create_board(int board_sz) { char **Board = (char **)malloc(board_sz * sizeof(char *)); for (int i = 0; i < board_sz; i++) { Board[i] = (char *)malloc(board_sz * sizeof(char)); } for (int i = 0; i < board_sz; i++) { for (int j = 0; j < board_sz; j++) { Board[i][j] = ' '; } } return Board; } // Returns true if the game is a draw int is_draw(char **Board, int board_sz)
  • 3. { for (int i = 0; i < board_sz; i++) { for (int j = 0; j < board_sz; j++) { if (Board[i][j] == ' ') { // empty square, so game ain't over yet return 0; } } } // no empty squares, so it's a draw return 1; } // Returns 'X' if (i,j) was a winning move for X // Returns 'O' if (i,j) was a winning move for O // Returns ASCII value 0 otherwise char winning_move(char **Board, int board_sz, int row_index, int col_index) { int win = 1; // check row for (int k = 0; k < board_sz; k++)
  • 4. { if (Board[row_index][k] != Board[row_index][col_index]) { win = 0; break; } } if (win) // means Board[i][k] == Board[i][j] { return Board[row_index][col_index]; } // check column win = 1; for (int k = 0; k < board_sz; k++) { if (Board[k][col_index] != Board[row_index][col_index]) { win = 0; break; } } if (win) {
  • 5. return Board[row_index][col_index]; } // check forward diagonal win = 1; for (int k = 0; k < board_sz; k++) { if (Board[k][k] != Board[row_index][col_index]) { win = 0; break; } } if (win) { return Board[row_index][col_index]; } // check reverse diagonal win = 1; for (int k = 0; k < board_sz; k++) { if (Board[k][board_sz - k - 1] != Board[row_index][col_index]) { win = 0;
  • 6. break; } } if (win) { return Board[row_index][col_index]; } // got nothing return 0; } // -------------------------getMove ------------------------------- int getMove( char ** Board, int board_sz, char *x, char *y ){ Board = create_board(board_sz); char winner = '0'; char row = *x; char col = *y; char turn = 'X'; char ch; int computer_enabled = 0; if (turn == 'X' || !computer_enabled) { printf("computer 'O' Moves are 'enabled'n"); printf("-Player's %c turn (qq to quit)nn", turn);
  • 7. // suggestion do { row = rand() % board_sz + 'a'; // col = rand() % board_sz + '0'; } while (Board[row - 'a'][col - '1'] != ' '); printf("*---> suggestion:t(%c %c)nn", row, col); printf("(X) Enter Move (row column) ---------------------------> "); fflush(stdout); scanf(" %c %c", &row, &col); // quit when enter qq. if (row == 'q' && col == 'q'){ exit(0); } } else { printf("computer 'O' Moves are 'enabled'n"); printf("*Player's O turn (qq to quit)nnn"); // Randomly pick a move do { row = rand() % board_sz + 'a'; //
  • 8. col = rand() % board_sz + '0'; } while (Board[row - 'a'][col - '1'] != ' '); printf("(O) computer Picks (%c %c) (hit a key to continue) ----> ", row, col); } // Make move if square is free int rowind = row - 'a'; int colind = col - '0'; if (rowind >= board_sz || colind >= board_sz) { printf("Invalid moven"); } else if (Board[rowind][colind] == ' ') { char enter; enter = getchar(); printf("Move is %c %c (%d, %d)n", row, col, rowind, colind); Board[rowind][colind] = turn; if (turn == 'X') { turn = 'O'; } else {
  • 9. turn = 'X'; } winner = winning_move(Board, board_sz, rowind, colind); } else { printf("Square is occupied; try again.n"); } // Game over - print Board & declare finish print_board(Board, board_sz); if (winner == 'X' || winner == 'O') { printf("Congratulations %c!n", winner); } else { printf("Game ends in a draw.n"); } // Free memory for (int i = 0; i < board_sz; i++) { free(Board[i]); }
  • 10. free(Board); return 0; } // ----------------------------------------------------------------------- // main function where program start int main(int argc, char **argv) { int opt; int board_sz = 3; int computer_enabled = 0; srand(time(NULL)); // Parse command line arguments using getopt() while ((opt = getopt(argc, argv, "s:i")) != -1) { switch (opt) { case 's': board_sz = atoi(optarg); break; case 'i': computer_enabled = 1; break; default:
  • 11. break; } } char **Board = create_board(board_sz); char winner = '0'; char *row; char *col; char turn = 'X'; char ch; // standard game loop while (!winner && !is_draw(Board, board_sz)) { print_board(Board, board_sz); getMove(Board, board_sz, *row, *col); return 0; } } this is tic-tac-toe game code in c. but i got a error here. could you please fix this code and explain why the error occurs here? please fix the code and show the run result also. thank you. C split_copy.c 2 incompatible integer to pointer conversion passing 'char' to parameter of type 'char *'; remove * [-Wint-conversion] gcc [Ln 273, Col 34] incompatible integer to pointer conversion passing 'char' to parameter of type 'char ; remove [ Wint-conversion] gcc [Ln 273, Col 40]