SlideShare a Scribd company logo
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include <string.h>
char** createboard(int size);
void printboard(char** board, int size);
int isdraw(char** board, int size);
char winningmove(char** board, int size, int i, int j);
// Prints the board
void printboard(char** board, int size) {
printf(" ");
for(int i = 1; i <= size; i++) {
printf("|%d", i);
}
printf("|n");
for(int i = 0; i < size; i++) {
printf("%c", 'a' + i);
for(int j = 0; j < size; j++) {
printf("|%c", board[i][j]);
}
printf("|n");
}
}
// Creates nxn tic tac toe board
char** createboard(int size) {
char** board = calloc(size, sizeof(char*));
for(int i = 0; i < size; i++) {
board[i] = calloc(size, sizeof(char));
}
for(int i = 0; i < size; i++) {
for(int j = 0; j < size; j++) {
board[i][j] = ' ';
}
}
return board;
}
// Returns true if the game is a draw
int isdraw(char** board, int size) {
for(int i = 0; i < size; i++) {
for(int j = 0; j < size; 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 winningmove(char** board, int size, int i, int j) {
char symbol = board[i][j];
bool win = true;
// check row
for(int k = 0; k < size; k++) {
if(board[i][k] != symbol) {
win = false;
break;
}
}
if(win) {
return symbol;
}
win = true;
// check column
for(int k = 0; k < size; k++) {
if(board[k][j] != symbol) {
win = false;
break;
}
}
if(win) {
return symbol;
}
win = true;
// check forward diagonal
for(int k = 0; k < size; k++) {
if(board[k][k] != symbol) {
win = false;
break;
}
}
if(win) {
return symbol;
}
win = true;
// check reverse diagonal
for(int k = 0; k < size; k++) {
if(board[k][size-k-1] != symbol) {
win = false;
break;
}
}
if(win) {
return symbol;
}
// got nothing
return 0;
}
int main(int argc, char *argv[]) {
int size = 3; // Default size is 3
int computer = 0; // Default is human vs human
srand(time(NULL));
// Parse command line arguments
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "-s") == 0) {
if (i+1 < argc) {
size = atoi(argv[i+1]);
if (size < 3) {
printf("Error: Board size cannot be less than 3n");
return 1;
}
i++;
} else {
printf("Error: Board size option requires a size argumentn");
return 1;
}
} else if (strcmp(argv[i], "-i") == 0) {
computer = 1;
}
}
char **board = createboard(size);
char winner = '0';
char row;
char col;
char turn = 'X';
// standard game loop
while (!winner && !isdraw(board, size)) {
printboard(board, size);
if (turn == 'X' || !computer) {
printf("Player %c's turnn", turn);
printf("(row column) Example: 1 2 ---> ");
fflush(stdout);
scanf(" %c %c", &row, &col);
} else {
printf("Computer's turnn");
// Randomly pick a move
do {
row = rand() % size + '1';
col = rand() % size + '1';
} while (board[row-'1'][col-'1'] != ' ');
printf("Computer played %c %cn", row, col);
}
// Make move if square is free
int rowind = row - '1';
int colind = col - '1';
if (rowind >= size || colind >= size) {
printf("Invalid moven");
} else if (board[rowind][colind] == ' ') {
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 = winningmove(board, rowind, colind, size);
} else {
printf("Square is occupied; try again.n");
}
}
// Game over - print board & declare finish
printboard(board, size);
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 < size; i++) {
free(board[i]);
}
free(board);
return 0;
}
this is the code that can play tic tac toe.
we can put command line argument -s <size> to set the dimensions of the board, and -i to
indicate that the computer picks its position randomly.
would you change the " // Parse command line arguments to using " part to using getopt() with
the same result?
please give a explain with the code and result.
#include -stdio-h- #include -stdlib-h- #include -stdbool-h- #include - (1).docx

More Related Content

PDF
The following is my code for a connectn program. When I run my code .pdf
PDF
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
DOCX
NewTetrisScore.cppNewTetrisScore.cpp newTetris.cpp  Defines t.docx
PDF
ANS#include iostream int Rn_get(int low, int high); int cva.pdf
PDF
VTU Data Structures Lab Manual
PDF
i have a runable code below that works with just guessing where the .pdf
PDF
This is the Java code i have for a Battleship project i am working o.pdf
The following is my code for a connectn program. When I run my code .pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
NewTetrisScore.cppNewTetrisScore.cpp newTetris.cpp  Defines t.docx
ANS#include iostream int Rn_get(int low, int high); int cva.pdf
VTU Data Structures Lab Manual
i have a runable code below that works with just guessing where the .pdf
This is the Java code i have for a Battleship project i am working o.pdf

Similar to #include -stdio-h- #include -stdlib-h- #include -stdbool-h- #include - (1).docx (14)

PDF
import javautilLinkedList import javautilQueue import .pdf
PDF
include ltiostreamgt include ltstringgt include .pdf
PDF
i have a code that runs, but it only lets the player to guess where .pdf
DOCX
i need an input of this program.  anything good or bad.  what could .docx
PDF
include ltstdiohgt include ltstdlibhgt include .pdf
PDF
The following code, is a one player battleship game in JAVA. Im tryi.pdf
PDF
Here is the code for youimport java.util.Scanner; import java.u.pdf
PDF
#include stdio.h #include string.h #include stdlib.h #in.pdf
PDF
#include stdio.h#include stdlib.h#include string.h#inclu.pdf
PDF
import java.util.Scanner;public class Main {    public static in.pdf
DOCX
Hangman Game Programming in C (coding)
PDF
The main class of the tictoe game looks like.public class Main {.pdf
import javautilLinkedList import javautilQueue import .pdf
include ltiostreamgt include ltstringgt include .pdf
i have a code that runs, but it only lets the player to guess where .pdf
i need an input of this program.  anything good or bad.  what could .docx
include ltstdiohgt include ltstdlibhgt include .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
#include stdio.h #include string.h #include stdlib.h #in.pdf
#include stdio.h#include stdlib.h#include string.h#inclu.pdf
import java.util.Scanner;public class Main {    public static in.pdf
Hangman Game Programming in C (coding)
The main class of the tictoe game looks like.public class Main {.pdf
Ad

More from PiersRCoThomsonw (20)

DOCX
-Complete the following Biology I questions for section a- (attached b.docx
DOCX
-Calculating Probability from Data- The table below gives details of s.docx
DOCX
- H2-4- (pytypes - py) Python's type (obj) function retums the type of.docx
DOCX
-Collenchyma -Cambium -Parenchyma -Vascular tissue system -Sclereid.docx
DOCX
-Analyze the effects of the COVVI-19 pandemic on food (interventions).docx
DOCX
-Analyze the effects of the COVVI-19 pandemic on (interventions) agro-.docx
DOCX
-1-_______________- 10- What is the amount of Siena and Kendall's reti.docx
DOCX
--Book-java package bookStore- public class Book extends Product { (1).docx
DOCX
---HINT- Consider the case when n-3- Professor Kelp decides to write a.docx
DOCX
(2 points) What is the Boolean expression for P- Design a circuit that.docx
DOCX
- Use a paragraph to reflect upon why you are interested in the chosen.docx
DOCX
- With your partner- list at least two tangible resources- two intangi.docx
DOCX
- List the following in order of precedence from highest precedence to.docx
DOCX
- Execute Summary - Overall paragraph of the the numer of devices that.docx
DOCX
- Climate change is a very big deai as it affects everyone- but not al.docx
DOCX
- Annual Demand -2-000 units - Days per year considered in average dai.docx
DOCX
(Mutually exclusive projects and NPV) You have been assigned the task.docx
DOCX
(1) Nonsidering aboue ER dingram- (a) Poovide the relational scbema (b.docx
DOCX
(22 pts) Derive a complexity function T(n) for the following pseudo co.docx
DOCX
(1 point) Construct a simple graph with vertices H-I-J-K whose degrees.docx
-Complete the following Biology I questions for section a- (attached b.docx
-Calculating Probability from Data- The table below gives details of s.docx
- H2-4- (pytypes - py) Python's type (obj) function retums the type of.docx
-Collenchyma -Cambium -Parenchyma -Vascular tissue system -Sclereid.docx
-Analyze the effects of the COVVI-19 pandemic on food (interventions).docx
-Analyze the effects of the COVVI-19 pandemic on (interventions) agro-.docx
-1-_______________- 10- What is the amount of Siena and Kendall's reti.docx
--Book-java package bookStore- public class Book extends Product { (1).docx
---HINT- Consider the case when n-3- Professor Kelp decides to write a.docx
(2 points) What is the Boolean expression for P- Design a circuit that.docx
- Use a paragraph to reflect upon why you are interested in the chosen.docx
- With your partner- list at least two tangible resources- two intangi.docx
- List the following in order of precedence from highest precedence to.docx
- Execute Summary - Overall paragraph of the the numer of devices that.docx
- Climate change is a very big deai as it affects everyone- but not al.docx
- Annual Demand -2-000 units - Days per year considered in average dai.docx
(Mutually exclusive projects and NPV) You have been assigned the task.docx
(1) Nonsidering aboue ER dingram- (a) Poovide the relational scbema (b.docx
(22 pts) Derive a complexity function T(n) for the following pseudo co.docx
(1 point) Construct a simple graph with vertices H-I-J-K whose degrees.docx
Ad

Recently uploaded (20)

PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
GDM (1) (1).pptx small presentation for students
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Complications of Minimal Access Surgery at WLH
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
A systematic review of self-coping strategies used by university students to ...
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Institutional Correction lecture only . . .
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
O7-L3 Supply Chain Operations - ICLT Program
GDM (1) (1).pptx small presentation for students
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Complications of Minimal Access Surgery at WLH
2.FourierTransform-ShortQuestionswithAnswers.pdf
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
A systematic review of self-coping strategies used by university students to ...
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Final Presentation General Medicine 03-08-2024.pptx
Supply Chain Operations Speaking Notes -ICLT Program
human mycosis Human fungal infections are called human mycosis..pptx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
VCE English Exam - Section C Student Revision Booklet
Anesthesia in Laparoscopic Surgery in India
Institutional Correction lecture only . . .

#include -stdio-h- #include -stdlib-h- #include -stdbool-h- #include - (1).docx

  • 1. #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <time.h> #include <string.h> char** createboard(int size); void printboard(char** board, int size); int isdraw(char** board, int size); char winningmove(char** board, int size, int i, int j); // Prints the board void printboard(char** board, int size) { printf(" "); for(int i = 1; i <= size; i++) { printf("|%d", i); } printf("|n"); for(int i = 0; i < size; i++) { printf("%c", 'a' + i); for(int j = 0; j < size; j++) { printf("|%c", board[i][j]); } printf("|n"); }
  • 2. } // Creates nxn tic tac toe board char** createboard(int size) { char** board = calloc(size, sizeof(char*)); for(int i = 0; i < size; i++) { board[i] = calloc(size, sizeof(char)); } for(int i = 0; i < size; i++) { for(int j = 0; j < size; j++) { board[i][j] = ' '; } } return board; } // Returns true if the game is a draw int isdraw(char** board, int size) { for(int i = 0; i < size; i++) { for(int j = 0; j < size; j++) { if(board[i][j] == ' ') { // empty square, so game ain't over yet return 0; } }
  • 3. } // 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 winningmove(char** board, int size, int i, int j) { char symbol = board[i][j]; bool win = true; // check row for(int k = 0; k < size; k++) { if(board[i][k] != symbol) { win = false; break; } } if(win) { return symbol; } win = true; // check column for(int k = 0; k < size; k++) {
  • 4. if(board[k][j] != symbol) { win = false; break; } } if(win) { return symbol; } win = true; // check forward diagonal for(int k = 0; k < size; k++) { if(board[k][k] != symbol) { win = false; break; } } if(win) { return symbol; } win = true; // check reverse diagonal for(int k = 0; k < size; k++) { if(board[k][size-k-1] != symbol) {
  • 5. win = false; break; } } if(win) { return symbol; } // got nothing return 0; } int main(int argc, char *argv[]) { int size = 3; // Default size is 3 int computer = 0; // Default is human vs human srand(time(NULL)); // Parse command line arguments for (int i = 1; i < argc; i++) { if (strcmp(argv[i], "-s") == 0) { if (i+1 < argc) { size = atoi(argv[i+1]); if (size < 3) { printf("Error: Board size cannot be less than 3n"); return 1; }
  • 6. i++; } else { printf("Error: Board size option requires a size argumentn"); return 1; } } else if (strcmp(argv[i], "-i") == 0) { computer = 1; } } char **board = createboard(size); char winner = '0'; char row; char col; char turn = 'X'; // standard game loop while (!winner && !isdraw(board, size)) { printboard(board, size); if (turn == 'X' || !computer) { printf("Player %c's turnn", turn); printf("(row column) Example: 1 2 ---> "); fflush(stdout); scanf(" %c %c", &row, &col); } else {
  • 7. printf("Computer's turnn"); // Randomly pick a move do { row = rand() % size + '1'; col = rand() % size + '1'; } while (board[row-'1'][col-'1'] != ' '); printf("Computer played %c %cn", row, col); } // Make move if square is free int rowind = row - '1'; int colind = col - '1'; if (rowind >= size || colind >= size) { printf("Invalid moven"); } else if (board[rowind][colind] == ' ') { 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 = winningmove(board, rowind, colind, size); } else {
  • 8. printf("Square is occupied; try again.n"); } } // Game over - print board & declare finish printboard(board, size); 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 < size; i++) { free(board[i]); } free(board); return 0; } this is the code that can play tic tac toe. we can put command line argument -s <size> to set the dimensions of the board, and -i to indicate that the computer picks its position randomly. would you change the " // Parse command line arguments to using " part to using getopt() with the same result? please give a explain with the code and result.