SHERIDAN, Wyo. — The soldiers were about to storm the fortress when they suddenly went still. James Smith, 17, and his teacher, Shirley Coulter, squinted at the desktop monitor.

James was programming his own military game, the final project in his Advanced Placement computer science principles class at Sheridan High School, here in the foothills of the Bighorn Mountains. Users competed as countries, like Israel or Japan, to take over a castle. But the game had crashed, and neither James nor Ms. Coulter — a 19-year veteran whose background is in teaching business classes — could figure out how to debug it.

“I’m learning with the kids,” she said. “They grasp it faster than I do.”

Ms. Coulter is one of hundreds of teachers in this sparsely populated state tasked with carrying out one of the most ambitious curriculum reform laws in the nation. Dozens of states have taken steps in recent years to expand students’ access to computer science, but last year, Wyoming became one of the few to require that all K-12 public schools offer it.

The mandate is part of a wide-ranging package of new laws, passed by the State Legislature last year, that is intended to wean Wyoming off its heavy reliance on the oil, gas and coal industries, and stem the flow of young people leaving for better jobs. Both major political parties have embraced the effort, as have tech companies eager to promote a national vision of rural economic revival built on coding skills.